求助,关于蓝牙持续ios 向蓝牙发送数据包

匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。||||||||||||||||||
查看: 262|回复: 8
在线时间538 小时经验值808 最后登录注册时间帖子阅读权限1UID1384958
头像被屏蔽
该用户从未签到
G币321 最后登录注册时间
提示: 作者被禁止或删除 内容自动屏蔽,只有管理员或有管理权限的成员可见
我在n7在&&设置——无线和网络——更多——中没有 绑定与移动热点这一个选项,是需要安装什么软件吗?我是看
快捷实现手机与N7的蓝牙数据共享_无需第三方软件_均无需Root
希望有高手指点
在线时间538 小时经验值808 最后登录注册时间帖子阅读权限1UID1384958
头像被屏蔽
该用户从未签到
G币321 最后登录注册时间
提示: 作者被禁止或删除 内容自动屏蔽,只有管理员或有管理权限的成员可见
我的是4.1.2
在线时间538 小时经验值808 最后登录注册时间帖子阅读权限1UID1384958
头像被屏蔽
该用户从未签到
G币321 最后登录注册时间
提示: 作者被禁止或删除 内容自动屏蔽,只有管理员或有管理权限的成员可见
图片是这样的
(14.05 KB)
16:18 上传
点击文件名下载附件
在线时间240 小时经验值279 最后登录注册时间帖子阅读权限50UID1291902
大学专科, 积分 279, 距离下一级还需 121 积分
该用户从未签到
G币58 最后登录注册时间
少年,仔细看下人家的帖子怎么写的,仔细看下。
在线时间1 小时经验值97 最后登录注册时间帖子阅读权限30UID2379811
初中生, 积分 97, 距离下一级还需 3 积分
该用户从未签到
G币26 最后登录注册时间
这个绑定是在手机端设置。很多手机因为定制版本的关系,没有这个选项。
在线时间2 小时经验值103 最后登录注册时间帖子阅读权限1UID
头像被屏蔽
该用户从未签到
G币0 最后登录注册时间
提示: 作者被禁止或删除 内容自动屏蔽,只有管理员或有管理权限的成员可见
我的是4.1.2
签名被屏蔽
在线时间598 小时经验值572 最后登录注册时间帖子阅读权限60UID1592661
大学本科, 积分 572, 距离下一级还需 128 积分
TA的每日心情慵懒 22:32签到天数: 1 天[LV.1]初来乍到
G币781 最后登录注册时间
骚年,那么大的图,那么详细的文字都看不明白吗?
在线时间918 小时经验值1413 最后登录注册时间帖子阅读权限80UID754080
研究生, 积分 1413, 距离下一级还需 287 积分
该用户从未签到
G币118 最后登录注册时间
N7的蓝牙---&配对的手机【右边】---&点进去有互联网访问,勾上就能上网。
如果没有这个选项是手机不支持,这时候可以下无线共享这个软件,手机安装后,先删除蓝牙配对,在打开无线共享这个软件后,再把N7和手机进行配对,N7蓝牙里面就能显示出互联网访问这个。
手机设置无线共享为蓝牙共享,OK。N7勾上互联网访问,上网吧。
如果rom不支持的话,蓝牙连上后手机会重启。。
这个是我用的,虽然不是最新的,但是亲测可用。
(319.16 KB)
18:31 上传
点击文件名下载附件
在线时间0 小时经验值93 最后登录注册时间帖子阅读权限30UID
初中生, 积分 93, 距离下一级还需 7 积分
该用户从未签到
G币93 最后登录注册时间
好心人,看得暖暖的
浏览过的版块
Powered by23:21 提问
求助蓝牙数据流读取问题,buffer的状态是一个迷
各位大大好,我是java小白+安卓小白,最近遇到一个头疼的问题:
我在创建蓝牙连接之后,开启新线程读取数据:(以下为线程类里的代码)
Thread ReadThread=new Thread(){
//开辟缓冲区buffer
byte[] buffer = new byte[1024];
int num=0;
public void run(){
while(count==0)
Log.e(TAG, "等待");
count =is1.available();
while(true){
Log.e(TAG, "读入数据");
Log.e(TAG, "k="+k);
num = inputstream.read(buffer);
//读入数据
Log.e("TAG", "num="+num);
Log.e("TAG", "buffer="+buffer);
String ss=toHexString1(buffer);//调用byte--&string方法
//data=inputstream.read();
if(is1.available()==0){
Log.e("TAG", "break");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
k是inputStream.read()执行的次数,num是读取进来的字节数量
大概就这样执行12次左右(k++到12,11次num显示为1024,最后一次只有30~50),logcat 输出“break”,也就是说此时is1.available()==0
可是我的蓝牙数据是一直不停得发送啊,求问各位大大为什么呢?
还有就是,有一次我在if(is1.available()==0)之前写了data=is1.read();就出现了
k++不停,而每一次num都是有几十,这时候数据还可靠吗?为什么会出现这种情况呢?
其他相似问题查看: 7225|回复: 5
求助~如何读取蓝牙串口数据,并对数据处理显示?
该用户从未签到主题帖子e币
没有eoe的账号,级别还太低,出门如何吹牛逼?
才可以下载或查看,没有帐号?
现有蓝牙从模块,想利用android手机的蓝牙功能实现与模块的连接,读取蓝牙串口的数据(手机给蓝牙模块发送16进制指令,蓝牙模块会自动的返回数据),并对接收到的数处理。& &&&
刚接触求助大家指教。。。该开发过程应该如何入手?有什么相关资料或者源码?我的想法是效仿“蓝牙串口助手”这个软件&&功能1 搜索蓝牙设备并配对&&2发送指令读取接收到的数据
该用户从未签到主题帖子e币
你两个功能不是想清楚了么,做吧。
该用户从未签到主题帖子e币
此问题依然在关注中...
该用户从未签到主题帖子e币
本帖最后由 kōbě¢卝焘 于
16:11 编辑
[mw_shl_code=java,true]package com.example.bluetooth_
import java.io.IOE
import java.lang.reflect.InvocationTargetE
import java.lang.reflect.M
import java.util.ArrayL
import java.util.L
import java.util.UUID;
import android.app.A
import android.bluetooth.BluetoothA
import android.bluetooth.BluetoothD
import android.bluetooth.BluetoothServerS
import android.bluetooth.BluetoothS
import android.content.BroadcastR
import android.content.C
import android.content.I
import android.content.IntentF
import android.os.B
import android.util.L
import android.view.V
import android.widget.AdapterV
import android.widget.ArrayA
import android.widget.B
import android.widget.ListV
import android.widget.T
import android.widget.ToggleB
public class testBlueTooth extends Activity {
& && &&&static final String SPP_UUID = &0-805F9B34FB&;
& && &&&private UUID
& && &&&private static final String TAG = &BluetoothTest&;& && &&&
& && &&&private static final boolean STATE_CONNECTED =
& && &&&private Button btnSearch, btnDis, btnE
& && &&&private ToggleButton tbtnS
& && &&&private ListView lvBTD
& && &&&private ArrayAdapter&String& adtD
& && &&&private List&String& lstDevices = new ArrayList&String&();& && &&&
& && &&&private BluetoothAdapter btA
& && &&&public static BluetoothSocket socket =
& && &&&public static BluetoothSocket btS
& && &&&public static AcceptThread serverT
& && &&&@Override
& && &&&public void onCreate(Bundle savedInstanceState) {
& && && && && & super.onCreate(savedInstanceState);
& && && && && & setContentView(R.layout.activity_main);
& && && && && & // Button 设置
& && && && && & btnSearch = (Button) this.findViewById(R.id.btnSearch);
& && && && && & btnSearch.setOnClickListener(new ClickEvent());
& && && && && &
& && && && && & btnExit = (Button) this.findViewById(R.id.btnExit);
& && && && && & btnExit.setOnClickListener(new ClickEvent());
& && && && && &
& && && && && & btnDis = (Button) this.findViewById(R.id.btnDis);
& && && && && & btnDis.setOnClickListener(new ClickEvent());
& && && && && & // ToogleButton设置
& && && && && & tbtnSwitch = (ToggleButton) this.findViewById(R.id.tbtnSwitch);
& && && && && & tbtnSwitch.setOnClickListener(new ClickEvent());
& && && && && & // ListView及其数据源 适配器
& && && && && & lvBTDevices = (ListView) this.findViewById(R.id.lvDevices);
& && && && && & adtDevices = new ArrayAdapter&String&(testBlueTooth.this,
& && && && && && && && && && &&&android.R.layout.simple_list_item_1, lstDevices);
& && && && && & lvBTDevices.setAdapter(adtDevices);
& && && && && & lvBTDevices.setOnItemClickListener(new ItemClickEvent());& && &&&
& && && && && &
& && && && && &
& && && && && & btAdapt = BluetoothAdapter.getDefaultAdapter();// 初始化本机蓝牙功能
& && && && && & uuid = UUID.fromString(&0-805F9B34FB&);
& && && && && &
& && && && && &
& && && && && & if(btAdapt == null){
& && && && && && && && &Log.e(TAG, &No BlueToothDevice!&);
& && && && && && && && &finish();
& && && && && && && && &
& && && && && & }
& && && && && & else{
& && && && && &
& && && && && && && && &if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)// 读取蓝牙状态并显示
& && && && && && && && && && &&&{
& && && && && && && && && && &&&tbtnSwitch.setChecked(true);
& && && && && && && && && && &&&Toast.makeText(testBlueTooth.this, &蓝牙尚未打开,服务端需先打开蓝牙&, Toast.LENGTH_LONG).show();
& && && && && && && && && && &&&
& && && && && && && && && && &&&}
& && && && && && && && &else if (btAdapt.getState() == BluetoothAdapter.STATE_ON){
& && && && && && && && &
& && && && && && && && && && &&&tbtnSwitch.setChecked(false);
& && && && && && && && && && &&&//服务端监听
& && && && && && && && && && &&&serverThread=new AcceptThread();
& && && && && && && && && && &&&serverThread.start();
& && && && && && && && && && &&&
& && && && && && && && &}
& && && && && && && && &// 注册Receiver来获取蓝牙设备相关的结果
& && && && && && && && &IntentFilter intent = new IntentFilter();
& && && && && && && && &intent.addAction(BluetoothDevice.ACTION_FOUND);// 用BroadcastReceiver来取得搜索结果
& && && && && && && && &intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
& && && && && && && && &intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
& && && && && && && && &intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
& && && && && && && && &registerReceiver(searchDevices, intent);
& && && && && &
& && && && && & }& && && && && && && && &
& && &&&private void manageConnectedSocket() {
& && && && && & //setTitle(&检测到蓝牙接入!&);
& && && && && & btSocket=
& && && && && & //打开控制继电器实例
& && && && && & Intent intent = new Intent();
& && && && && & intent.setClass(testBlueTooth.this, RelayControl.class);
& && && && && & startActivity(intent);
& && &&&private BroadcastReceiver searchDevices = new BroadcastReceiver() {
& && && && && & public void onReceive(Context context, Intent intent) {
& && && && && && && && &String action = intent.getAction();
& && && && && && && && &Bundle b = intent.getExtras();
& && && && && && && && &Object[] lstName = b.keySet().toArray();
& && && && && && && && &// 显示所有收到的消息及其细节
& && && && && && && && &for (int i = 0; i & lstName. i++) {
& && && && && && && && && && &&&String keyName = lstName.toString();
& && && && && && && && && && &&&Log.e(keyName, String.valueOf(b.get(keyName)));
& && && && && && && && &}
& && && && && && && && &//搜索设备时,取得设备的MAC地址
& && && && && && && && &if (BluetoothDevice.ACTION_FOUND.equals(action)) {
& && && && && && && && && && &&&BluetoothDevice device = intent
& && && && && && && && && && && && && && && && &.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
& && && && && && && && && && &&&String str= device.getName() + &|& + device.getAddress();
& && && && && && && && && && &&&
& && && && && && && && && && &&&if (lstDevices.indexOf(str) == -1)// 防止重复添加
& && && && && && && && && && && && && & lstDevices.add(str); // 获取设备名称和mac地址
& && && && && && && && && && &&&adtDevices.notifyDataSetChanged();
& && && && && && && && &}
& && && && && & }
& && &&&};
& && &&&@Override
& && &&&protected void onDestroy() {
& && && && &this.unregisterReceiver(searchDevices);
& && && && && & super.onDestroy();
& && && && && & android.os.Process.killProcess(android.os.Process.myPid());
& && && && && & serverThread.cancel();
& && && && && & serverThread.destroy();
& && && && && &
& && &&&class ItemClickEvent implements AdapterView.OnItemClickListener {
& && && && && & @Override
& && && && && & public void onItemClick(AdapterView&?& arg0, View arg1, int arg2,
& && && && && && && && && && &&&long arg3) {
& && && && && && && && &
& && && && && && && && &String str = lstDevices.get(arg2);
& && && && && && && && &String[] values = str.split(&\\|&);
& && && && && && && && &String address=values[1];
& && && && && && && && &Log.e(&address&,values[1]);
& && && && && && && && &
& && && && && && &&&uuid = UUID.fromString(SPP_UUID);
& && && && && && && && &Log.e(&uuid&,uuid.toString());
& && && && && && && && &
& && && && && && && && &BluetoothDevice btDev = btAdapt.getRemoteDevice(address);//&00:11:00:18:05:45&
& && && && && &
& && && && && && && && && && &&&M
& && && && && && && && && && &&&try {
& && && && && && && && && && && && && & m = btDev.getClass().getMethod(&createRfcommSocket&, new Class[] {int.class});
& && && && && && && && && && && && && & btSocket = (BluetoothSocket) m.invoke(btDev, Integer.valueOf(1));
& && && && && && && && && && &&&} catch (SecurityException e1) {
& && && && && && && && && && && && && & // TODO Auto-generated catch block
& && && && && && && && && && && && && & e1.printStackTrace();
& && && && && && && && && && &&&} catch (NoSuchMethodException e1) {
& && && && && && && && && && && && && & // TODO Auto-generated catch block
& && && && && && && && && && && && && & e1.printStackTrace();
& && && && && && && && && && &&&} catch (IllegalArgumentException e) {
& && && && && && && && && && && && && & // TODO Auto-generated catch block
& && && && && && && && && && && && && & e.printStackTrace();
& && && && && && && && && && &&&} catch (IllegalAccessException e) {
& && && && && && && && && && && && && & // TODO Auto-generated catch block
& && && && && && && && && && && && && & e.printStackTrace();
& && && && && && && && && && &&&} catch (InvocationTargetException e) {
& && && && && && && && && && && && && & // TODO Auto-generated catch block
& && && && && && && && && && && && && & e.printStackTrace();
& && && && && && && && && && &&&}
& && && && && && && && && &&&
& && && && && && && && && && &&&
& && && && && && && && &btAdapt.cancelDiscovery();
& && && && && && && && &try {
& && && && && && && && && && &&&//btSocket = btDev.createRfcommSocketToServiceRecord(uuid);& && && && && && && && && && &&&
& && && && && && && && && && &&&btSocket.connect();& && && && && && && && && && &&&
& && && && && && && && && && &&&Log.e(TAG, & BT connection established, data transfer link open.&);
& && && && && && && && && && &&&
& && && && && && && && && && &&&Toast.makeText(testBlueTooth.this, &连接成功,进入控制界面&, Toast.LENGTH_SHORT).show();
& && && && && && && && && && &&&//setTitle(&连接成功&);
& && && && && && && && && && &&&
& && && && && && && && && && &&&//打开控制继电器实例
& && && && && && && && && && &&&Intent intent = new Intent();
& && && && && && && && && && &&&intent.setClass(testBlueTooth.this, RelayControl.class);
& && && && && && && && && && &&&startActivity(intent);
& && && && && && && && && && &&&
& && && && && && && && &} catch (IOException e) {
& && && && && && && && && && &&&// TODO Auto-generated catch block
& && && && && && && && && && &&&Log.e(TAG, & Connection failed.&, e);& && &&&
& && && && && && && && && && &&&//Toast.makeText(getApplicationContext(), &连接失败&, Toast.LENGTH_SHORT);
& && && && && && && && && && &&&setTitle(&连接失败..&);
& && && && && && && && &}& && &&&
& && && && && &
private& && &&&class ClickEvent implements View.OnClickListener {
& && && && && & @Override
& && && && && & public void onClick(View v) {
& && && && && && && && &if (v == btnSearch)// 搜索蓝牙设备,在BroadcastReceiver显示结果
& && && && && && && && &{
& && && && && && && && && && &&&if (btAdapt.getState() == BluetoothAdapter.STATE_OFF) {// 如果蓝牙还没开启
& && && && && && && && && && && && && & Toast.makeText(testBlueTooth.this, &请先打开蓝牙&, 1000).show();
& && && && && && && && && && && && && &
& && && && && && && && && && &&&}
& && && && && && && && && && &&&setTitle(&本机蓝牙地址:& + btAdapt.getAddress());
& && && && && && && && && && &&&lstDevices.clear();
& && && && && && && && && && &&&btAdapt.startDiscovery();
& && && && && && && && &} else if (v == tbtnSwitch) {// 本机蓝牙启动/关闭
& && && && && && && && && && &&&if (tbtnSwitch.isChecked() == false)& && && && && && && && && && &&&
& && && && && && && && && && &&&{
& && && && && && && && && && && && && &
& && && && && && && && && && && && && && && && &btAdapt.enable();
& && && && && && && && && && && && && && &&&try {
& && && && && && && && && && && && && && && && && && &&&Thread.sleep(5 * 1000);//延时5s
& && && && && && && && && && && && && && && && &} catch (InterruptedException e) {
& && && && && && && && && && && && && && && && && && &&&// TODO Auto-generated catch block
& && && && && && && && && && && && && && && && && && &&&e.printStackTrace();
& && && && && && && && && && && && && && && && &} //暂停1秒钟
& && && && && && && && && && && && && && && && &//服务端监听
& && && && && && && && && && && && && && && && &serverThread=new AcceptThread();
& && && && && && && && && && && && && && && && &serverThread.start();
& && && && && && && && && && && && && && && && &Toast.makeText(testBlueTooth.this, &服务端监听已打开&, 1000).show();
& && && && && && && && && && && && && && && && &
& && && && && && && && && && &&&}& && && && && &
& && && && && && && && && && &&&
& && && && && && && && && && &&&else if (tbtnSwitch.isChecked() == true)
& && && && && && && && && && && && && & btAdapt.disable();
& && && && && && && && &} else if (v == btnDis)// 本机可以被搜索
& && && && && && && && &{
& && && && && && && && && && &&&Intent discoverableIntent = new Intent(
& && && && && && && && && && && && && && && && &BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
& && && && && && && && && && &&&discoverableIntent.putExtra(
& && && && && && && && && && && && && && && && &BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
& && && && && && && && && && &&&startActivity(discoverableIntent);
& && && && && && && && &} else if (v == btnExit) {
& && && && && && && && && && &&&try {
& && && && && && && && && && && && && & if (btSocket != null)
& && && && && && && && && && && && && && && && &btSocket.close();
& && && && && && && && && && &&&} catch (IOException e) {
& && && && && && && && && && && && && & e.printStackTrace();
& && && && && && && && && && &&&}
& && && && && && && && && && &&&testBlueTooth.this.finish();
& && && && && && && && &}
& && && && && & }
class AcceptThread extends Thread {
& && &&&private final BluetoothServerSocket serverS
& & public AcceptThread() {
& && &&&// Use a temporary object that is later assigned to mmServerSocket,
& && &&&// because mmServerSocket is final& && &&&
& && && && &BluetoothServerSocket tmp=
& && && && &try {
& && && && && && && && &//tmp = btAdapt.listenUsingRfcommWithServiceRecord(&MyBluetoothApp&, uuid);
& && && && && && && && &
& && && && && && && && &Log.e(TAG, &++BluetoothServerSocket established!++&);
& && && && && && && && &Method listenMethod = btAdapt.getClass().getMethod(&listenUsingRfcommOn&, new Class[]{int.class});
& && && && && && && && &tmp = ( BluetoothServerSocket) listenMethod.invoke(btAdapt, Integer.valueOf( 1));
& && && && && && && && &
& && && && && & } catch (SecurityException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &e.printStackTrace();
& && && && && & } catch (IllegalArgumentException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &e.printStackTrace();
& && && && && & } catch (NoSuchMethodException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &e.printStackTrace();
& && && && && & } catch (IllegalAccessException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &e.printStackTrace();
& && && && && & } catch (InvocationTargetException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &e.printStackTrace();
& && && && && & }
& && && && && &
& && && && && & serverSocket=
& & public void run() {
& && &&&// Keep listening until exception occurs or a socket is returned
& && && && && &//mState!=STATE_CONNECTED
& && &&&while(true) {
& && && && &try {
& && && && && & socket = serverSocket.accept();
& && && && && & Log.e(TAG, &++BluetoothSocket established! DataLink open.++&);
& && && && &} catch (IOException e) {
& && && && && &
& && && && &}
& && && && &// If a connection was accepted
& && && && &if (socket != null) {
& && && && && & // Do work to manage the connection (in a separate thread)
& && && && && & manageConnectedSocket();& &&&
& && && && && & try {
& && && && && && && && && && && && && & serverSocket.close();
& && && && && && && && && && &&&} catch (IOException e) {
& && && && && && && && && && && && && & // TODO Auto-generated catch block
& && && && && && && && && && && && && & e.printStackTrace();
& && && && && && && && && && &&&}
& && && && && &
& && && && &}
& & public void cancel() {
& && &&&try {
& && && && &serverSocket.close();
& && &&&} catch (IOException e) { }
}[/mw_shl_code][mw_shl_code=java,true]package com.example.bluetooth_
import java.io.FileOutputS
import java.io.IOE
import java.io.InputS
import java.io.OutputS
import java.io.UnsupportedEncodingE
import com.example.opengl.GLTutorialC
import com.example.opengl.M
import android.app.A
import android.content.I
import android.graphics.PixelF
import android.os.B
import android.os.H
import android.os.M
import android.util.L
import android.view.V
import android.view.W
import android.widget.B
import android.widget.EditT
import android.widget.TextV
import android.widget.T
public class RelayControl extends Activity&&{
& && &&&public static boolean isRecording =// 线程控制标记
& && &&&private Button releaseCtrl, btBack, btn_send,
& && &&&private OutputStream outStream =
& && &&&private EditText _txtR
& && &&&private ConnectedThread manageT
& && &&&private Handler mH
& && &&&private String encodeType = &GBK&;
& && &&&private String TAG = &Savetofile&;
& && &&&private FileOutputS
& && &&&private String filename = &/sdcard/save.txt&;
& && &&&private byte[]
& && &&&private String readStr1;
& && &&&@Override
& && &&&public void onCreate(Bundle savedInstanceState) {
& && && && && & super.onCreate(savedInstanceState);
& && && && && & setContentView(R.layout.relaycontrol);
& && && && && & // 接收线程启动
& && && && && & manageThread = new ConnectedThread();
& && && && && & mHandler = new MyHandler();
& && && && && & manageThread.Start();
& && && && && & findMyView();
& && && && && & setMyViewListener();
& && && && && & setTitle(&返回前需先关闭socket连接&);
& && && && && & // 接收区不可见
& && && && && & _txtRead.setCursorVisible(false); // 设置输入框中的光标不可见
& && && && && & _txtRead.setFocusable(false); // 无焦点
& && &&&private void findMyView() {
& && && && && & releaseCtrl = (Button) findViewById(R.id.button1);
& && && && && & btBack = (Button) findViewById(R.id.button2);
& && && && && & btn_send = (Button) findViewById(R.id.btn_send);
& && && && && & _txtRead = (EditText) findViewById(R.id.etShow);
& && && && && & opengl=(Button) findViewById(R.id.opengl);
& && &&&private void setMyViewListener() {
& && && && && & // 监听RadioButton
//& && && && && & radioType.setOnCheckedChangeListener(this);
//& && && && && & rbPC.setChecked(true);// 设置为默认选中为 PC通信
& && && && && & releaseCtrl.setOnClickListener(new ClickEvent());
& && && && && & btBack.setOnClickListener(new ClickEvent());
//& && && && && & btSend.setOnClickListener(new ClickEvent());
& && && && && & btn_send.setOnClickListener(new ClickEvent());
& && &&&@Override
& && &&&public void onDestroy() {
& && && && && & try {
& && && && && && && && &testBlueTooth.btSocket.close();
& && && && && & } catch (IOException e) {
& && && && && && && && &e.printStackTrace();
& && && && && & }
& && && && && & super.onDestroy();
& && &&&public class ClickEvent implements View.OnClickListener {
& && && && && & @Override
& && && && && & public void onClick(View v) {
& && && && && && && && &if (v == releaseCtrl)// 释放连接
& && && && && && && && &{
& && && && && && && && && && &&&try {
& && && && && && && && && && && && && & testBlueTooth.btSocket.close();
& && && && && && && && && && && && && & manageThread.Stop();
& && && && && && && && && && && && && & // testBlueTooth.serverThread.cancel();
& && && && && && && && && && && && && & // Toast.makeText(getApplicationContext(), &socket连接已关闭&,
& && && && && && && && && && && && && & // Toast.LENGTH_SHORT);
& && && && && && && && && && && && && & setTitle(&socket连接已关闭&);
& && && && && && && && && && &&&} catch (IOException e) {
& && && && && && && && && && && && && & // Log.e(TAG,&ON RESUME: Unable to close socket during connection failure&,e2);
& && && && && && && && && && && && && & // Toast.makeText(getApplicationContext(), &关闭连接失败&,
& && && && && && && && && && && && && & // Toast.LENGTH_SHORT).show();
& && && && && && && && && && && && && & setTitle(&关闭连接失败&);
& && && && && && && && && && &&&}
& && && && && && && && &} else if (v == btBack) {// 返回
& && && && && && && && && && &&&RelayControl.this.finish();
& && && && && && && && &}
//& && && && && && && && &else if (v == btSend) {
//& && && && && && && && && && &&&setTitle(&发送成功&);
//& && && && && && && && &}
& && && && && && && && &else if (v == btn_send) {
& && && && && && && && && && &&&try {
& && && && && && && && && && && && && & fos = new FileOutputStream(filename);
& && && && && && && && && && && && && & _txtRead = (EditText) findViewById(R.id.etShow);
& && && && && && && && && && && && && & fos.write(_txtRead.getText().toString().getBytes());
& && && && && && && && && && && && && & by=_txtRead.getText().toString().getBytes();
& && && && && && && && && && &&&} catch (Exception e) {
& && && && && && && && && && && && && & Log.d(TAG, e.toString());
& && && && && && && && && && &&&}
& && && && && && && && && && &&&Toast.makeText(RelayControl.this, &保存成功&, Toast.LENGTH_SHORT)
& && && && && && && && && && && && && && && && &.show();
& && && && && && && && &}
& && && && && && && && &else if (v == opengl) {
& && && && && && && && && && &&&System.out.println(&--------------------&);
& && && && && && && && && && &&&Intent intent=new Intent(RelayControl.this,com.example.opengl.opengl.class);
& && && && && && && && && && &&&Bundle bundle = new Bundle();& && && && && && && && && &//创建Bundle对象& &
& && && && && && && && && && &&&bundle.putString(&data&, readStr1);
& && && && && && && && && && &&&intent.putExtras(bundle);
& && && && && && && && && && &&&startActivity(intent);
& && && && && && && && &}
& && && && && & }
& && &&&public static void setEditTextEnable(TextView view, Boolean able) {
& && && && && & // view.setTextColor(R.color.read_only_color); //设置只读时的文字颜色
& && && && && & if (view instanceof android.widget.EditText) {
& && && && && && && && &view.setCursorVisible(able); // 设置输入框中的光标不可见
& && && && && && && && &view.setFocusable(able); // 无焦点
& && && && && && && && &view.setFocusableInTouchMode(able); // 触摸时也得不到焦点
& && && && && & }
& && &&&public void sendMessage(String message) {
& && && && && & // 控制模块
& && && && && & try {
& && && && && && && && &outStream = testBlueTooth.btSocket.getOutputStream();
& && && && && & } catch (IOException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &// Log.e(TAG, &ON RESUME: Output stream creation failed.&, e);
& && && && && && && && &Toast.makeText(getApplicationContext(),
& && && && && && && && && && && && && & & Output stream creation failed.&, Toast.LENGTH_SHORT);
& && && && && & }
& && && && && & byte[] msgBuffer =
& && && && && & try {
& && && && && && && && &msgBuffer = message.getBytes(encodeType);// 编码
& && && && && & } catch (UnsupportedEncodingException e1) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &e1.printStackTrace();
& && && && && && && && &Log.e(&write&, &Exception during write encoding GBK &, e1);
& && && && && & }
& && && && && & // while(true){
& && && && && & try {
& && && && && && && && &outStream.write(msgBuffer);
& && && && && && && && &// Toast.makeText(getApplicationContext(), &发送数据中..&,
& && && && && && && && &// Toast.LENGTH_SHORT);
& && && && && && && && &setTitle(&成功发送指令:& + message);
& && && && && & } catch (IOException e) {
& && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && &// Log.e(TAG, &ON RESUME: Exception during write.&, e);
& && && && && && && && &Toast.makeText(getApplicationContext(), &发送数据失败&,
& && && && && && && && && && && && && & Toast.LENGTH_SHORT);
& && && && && & }
& && &&&class ConnectedThread extends Thread {
& && && && && & private InputStream inStream =// 蓝牙数据输入流
& && && && && &
& && && && && & private T
& && && && && & public ConnectedThread() {
& && && && && && && && &isRecording =
& && && && && && && && &this.wait = 200;
& && && && && && && && &thread = new Thread(new ReadRunnable());
& && && && && & }
& && && && && & public void Stop() {
& && && && && && && && &isRecording =
& && && && && && && && &// thread.stop();
& && && && && && && && &// State bb = thread.getState();
& && && && && & }
& && && && && & public void Start() {
& && && && && && && && &isRecording =
& && && && && && && && &State aa = thread.getState();
& && && && && && && && &if (aa == State.NEW) {
& && && && && && && && && && &&&thread.start();
& && && && && && && && &} else
& && && && && && && && && && &&&thread.resume();
& && && && && & }
& && && && && & private class ReadRunnable implements Runnable {
& && && && && && && && &public void run() {
& && && && && && && && && && &&&while (isRecording) {
& && && && && && && && && && && && && & try {
& && && && && && && && && && && && && && && && &inStream = testBlueTooth.btSocket.getInputStream();
& && && && && && && && && && && && && & } catch (IOException e) {
& && && && && && && && && && && && && && && && &// TODO Auto-generated catch block
& && && && && && && && && && && && && && && && &// Log.e(TAG,
& && && && && && && && && && && && && && && && &// &ON RESUME: Output stream creation failed.&, e);
& && && && && && && && && && && && && && && && &Toast.makeText(getApplicationContext(),
& && && && && && && && && && && && && && && && && && && && && & & input stream creation failed.&,
& && && && && && && && && && && && && && && && && && && && && & Toast.LENGTH_SHORT);
& && && && && && && && && && && && && & }
& && && && && && && && && && && && && & // char[]dd= new char[40];
& && && && && && && && && && && && && & int length = 37;
& && && && && && && && && && && && && & byte[] temp = new byte[length];
& && && && && && && && && && && && && & // String readStr=&&;
& && && && && && && && && && && && && & // keep listening to InputStream while connected
& && && && && && && && && && && && && & if (inStream != null) {
& && && && && && && && && && && && && && && && &try {
& && && && && && && && && && && && && && && && && && &&&int len = inStream.read(temp, 0, length);
& && && && && && && && && && && && && && && && && && &&&Log.e(&available&, String.valueOf(len));
& && && && && && && && && && && && && && && && && && &&&// setTitle(&available&+len);
& && && && && && && && && && && && && && && && && && &&&if (len & 0) {
& && && && && && && && && && && && && && && && && && && && && & byte[] btBuf = new byte[len];
& && && && && && && && && && && && && && && && && && && && && & System.arraycopy(temp, 0, btBuf, 0,
& && && && && && && && && && && && && && && && && && && && && && && && && && &&&btBuf.length);
& && && && && && && && && && && && && && && && && && && && && & // for(int iii=0;iii&iii++){
& && && && && && && && && && && && && && && && && && && && && & // String out = new Character ((char)
& && && && && && && && && && && && && && && && && && && && && & // btBuf[iii]).toString();
& && && && && && && && && && && && && && && && && && && && && & // readStr = readStr +}
& && && && && && && && && && && && && && && && && && && && && & // InputStreamReader inputStreamReader = new
& && && && && && && && && && && && && && && && && && && && && & // InputStreamReader(inStream, &UTF-8&);
& && && && && && && && && && && && && && && && && && && && && & // int
& && && && && && && && && && && && && && && && && && && && && & // bb=inputStreamReader.read(dd,0,dd.length-1);
& && && && && && && && && && && && && && && && && && && && && & // String TempStr=new
& && && && && && && && && && && && && && && && && && && && && & // String(String.valueOf(dd).trim().getBytes());
& && && && && && && && && && && && && && && && && && && && && & // 读编码
& && && && && && && && && && && && && && && && && && && && && & readStr1 = new String(btBuf, encodeType);
& && && && && && && && && && && && && && && && && && && && && & System.out.println(readStr1+&--------&);
& && && && && && && && && && && && && && && && && && && && && & mHandler.obtainMessage(1, len, -1, readStr1)
& && && && && && && && && && && && && && && && && && && && && && && && && && &&&.sendToTarget();
& && && && && && && && && && && && && && && && && && &&&}
& && && && && && && && && && && && && && && && && && &&&Thread.sleep(wait);// 延时一定时间缓冲数据
& && && && && && && && && && && && && && && && &} catch (Exception e) {
& && && && && && && && && && && && && && && && && && &&&// TODO Auto-generated catch block
& && && && && && && && && && && && && && && && && && &&&mHandler.sendEmptyMessage(00);
& && && && && && && && && && && && && && && && &}
& && && && && && && && && && && && && & }
& && && && && && && && && && &&&}
& && && && && && && && &}
& && && && && & }
& && &&&public class MyHandler extends Handler {
& && && && && & @Override
& && && && && & public void dispatchMessage(Message msg) {
& && && && && && && && &switch (msg.what) {
& && && && && && && && &case 00:
& && && && && && && && && && &&&isRecording =
& && && && && && && && && && &&&_txtRead.setText(&&);
& && && && && && && && && && &&&_txtRead.setHint(&socket连接已关闭&);
& && && && && && && && && && &&&// _txtRead.setText(&inStream establishment Failed!&);
& && && && && && && && && && &&&
& && && && && && && && &case 01:
& && && && && && && && && && &&&String info = (String) msg.
& && && && && && && && && && &&&_txtRead.append(info);
& && && && && && && && && && &&&
& && && && && && && && &default:
& && && && && && && && && && &&&
& && && && && && && && &}
& && && && && & }
}[/mw_shl_code]这两段代码是蓝牙接收数据的代码,布局文件,也给你们吧
[mw_shl_code=java,true]&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
& & android:layout_width=&fill_parent&
& & android:layout_height=&fill_parent&
& & android:orientation=&vertical& &
& & &LinearLayout
& && &&&android:id=&@+id/LinearLayout01&
& && &&&android:layout_width=&wrap_content&
& && &&&android:layout_height=&wrap_content& &
& && &&&&TextView
& && && && &android:id=&@+id/TextView01&
& && && && &android:layout_width=&100dip&
& && && && &android:layout_height=&wrap_content&
& && && && &android:text=&手机蓝牙开关& &
& && &&&&/TextView&
& && &&&&ToggleButton
& && && && &android:id=&@+id/tbtnSwitch&
& && && && &android:layout_width=&wrap_content&
& && && && &android:layout_height=&wrap_content&
& && && && &android:text=&蓝牙开关& &
& && &&&&/ToggleButton&
& && &&&&Button
& && && && &android:id=&@+id/btnDis&
& && && && &android:layout_width=&160dip&
& && && && &android:layout_height=&wrap_content&
& && && && &android:text=&本机蓝牙可见& &
& && &&&&/Button&
& & &/LinearLayout&
& & &LinearLayout
& && &&&android:id=&@+id/LinearLayout02&
& && &&&android:layout_width=&wrap_content&
& && &&&android:layout_height=&wrap_content&
& && &&&android:orientation=&horizontal& &
& && &&&&Button
& && && && &android:id=&@+id/btnSearch&
& && && && &android:layout_width=&160sp&
& && && && &android:layout_height=&wrap_content&
& && && && &android:text=&搜索设备& &
& && &&&&/Button&
& && &&&&Button
& && && && &android:id=&@+id/btnExit&
& && && && &android:layout_width=&160sp&
& && && && &android:layout_height=&wrap_content&
& && && && &android:text=&退出程序& &
& && &&&&/Button&
& & &/LinearLayout&
& & &ListView
& && &&&android:id=&@+id/lvDevices&
& && &&&android:layout_width=&fill_parent&
& && &&&android:layout_height=&fill_parent& &
& & &/ListView&
&/LinearLayout&[/mw_shl_code]
[mw_shl_code=java,true]&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
& & android:layout_width=&match_parent&
& & android:layout_height=&match_parent&
& & android:layout_gravity=&center_horizontal&
& & android:orientation=&vertical& &
& & &LinearLayout
& && &&&android:id=&@+id/linear&
& && &&&android:layout_width=&match_parent&
& && &&&android:layout_height=&wrap_content&
& && &&&android:gravity=&center& &
& && &&&&Button
& && && && &android:id=&@+id/button1&
& && && && &android:layout_width=&100dip&
& && && && &android:layout_height=&wrap_content&
& && && && &android:layout_weight=&2&
& && && && &android:text=&关闭& /&
& && &&&&Button
& && && && &android:id=&@+id/button2&
& && && && &android:layout_width=&100dip&
& && && && &android:layout_height=&wrap_content&
& && && && &android:layout_weight=&2&
& && && && &android:text=&返回& /&
& && &&&&Button
& && && && &android:id=&@+id/btn_send&
& && && && &android:layout_width=&100dp&
& && && && &android:layout_height=&wrap_content&
& && && && &android:layout_weight=&2&
& && && && &android:text=&保存& /&
& & &/LinearLayout&
& & &EditText
& && &&&android:id=&@+id/etShow&
& && &&&android:layout_width=&match_parent&
& && &&&android:layout_height=&300dp&
& && &&&android:ems=&20&
& && &&&android:hint=&接收区&
& && &&&android:layout_below=&@+id/linear&
& && &&&android:inputType=&textMultiLine& /&
&/RelativeLayout&[/mw_shl_code]
图片我就发了,虚拟机上运行不出,只能真机测试
大神,能把完整的源代码包给我吗?&
签到天数: 70 天连续签到: 1 天[LV.6]常住居民II主题帖子e币
好像少了两个类 啊
该用户从未签到主题帖子e币
[mw_shl_code=java,true]package com.example.bluetooth_
import java.io.IOE
大神,能把完整的源代码包给我吗?
QQ已认证,此人靠谱
推荐阅读热门话题
61882420384327281281261260251226218210206203715
半小时前5&小时前昨天&23:39昨天&15:44昨天&14:15昨天&13:15昨天&13:13昨天&13:10昨天&13:09昨天&13:00昨天&11:46昨天&09:17前天&23:29前天&15:12前天&14:40前天&14:40
Powered by}

我要回帖

更多关于 android蓝牙发送数据 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信