检查设备是否支持蓝牙 实现步骤:
判断是否有蓝牙设备。 是否开启蓝牙。 若没有开启则请求开启蓝牙。 操作需要权限 :
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter(); if(null!=ba){ System.out.println("本机有蓝牙设备"); if(!ba.isEnabled()){ Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(intent); //或者ba.enable(); //同样的关闭WIFi为ba.disable(); }else{ System.out.println("本机没有蓝牙设备"); }设置蓝牙的可见时间(允许其他设备搜索到) 实现步骤: 设置蓝牙的可见时间 需要权限:
Intent intentvisible=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intentvisible.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400); //这个值么默认120秒,超过300秒将会被设置为300.可是在我的设备上是2400也行。API出错? this.startActivity(intentvisible);获取扫描到的设备 实现步骤: 调用开始扫描的代码 需要权限:
//开始扫描 BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter(); ba.startDiscovery(); //注册这个扫描广播 IntentFilter intentfilter=new IntentFilter(BluetoothDevice.ACTION_FOUND); MyReceiver mr=new MyReceiver(); this.registerReceiver(mr, intentfilter); //注册一个扫描完毕的广播 class FinishFound extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BluetoothActivity.this.unregisterReceiver(this); BluetoothActivity.this.unregisterReceiver(mr); } }获取已经配对的蓝牙设备(集合) 实现步骤: 获取集合。 需要权限:
Set蓝牙发送数据(发送字符串) 实现步骤:device=ba.getBondedDevices(); if(device.size()>0) { for(BluetoothDevice bd:device) { System.out.println(bd.getAddress()+bd.getName()); } }
发送字符串到一个配对的设备。 需要已知名和地址。
private void sendDataToPairedDevice(String message ,BluetoothDevice device){ byte[] toSend = message.getBytes(); try { UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(applicationUUID); OutputStream mmOutStream = socket.getOutputStream(); mmOutStream.write(toSend); // Your Data is sent to BT connected paired device ENJOY. } catch (IOException e) { Log.e(TAG, "Exception during write", e); } }调用以上的方法:
sendDataToPairedDevice("text to send" ,bluetoothDevice);蓝牙接收数据 实现步骤:
启动一个service去监听是否有数据返回。一旦有数据返回就启动一个线程去处理数据 处理完数据,通过广播去通知UI。 服务类
/** *负责监听启动应用程序 后的接收数据 */ public class ReceiveThread extends Service { private Socket socket; private String workStatus; // 当前工作状况,null表示正在处理 | success表示处理成功,failure表示处理失败 public static Boolean mainThreadFlag = true; //状态 @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } private void doListen() { Log.d("chl", "doListen()"); //开始监听 while (mainThreadFlag) { //开始监听数据 new Thread(new ThreadReadWriterSocketServer(ReceiveThread.this, socket)); } } }线程工作类
public class ThreadReadWriterSocketServer implements Runnable{ private Socket client=null; private Context context=null; public ThreadReadWriterSocketServer(Context context,Socket client) { this.context=context; this.client=client; } @Override public void run() { Receive(); } private void Receive() { //处理数据 } }十六进制转换 蓝牙通信需要通常转换十六进制进行,方法如下
public static String bytesToHexString(byte[] bytes) { String result = ""; for (int i = 0; i < bytes.length; i++) { String hexString = Integer.toHexString(bytes[i] & 0xFF); if (hexString.length() == 1) { hexString = '0' + hexString; } result += hexString.toUpperCase(); } return result; }
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (2)
- QQ805187A727F30446E837E1F6C8F3F5 2015-12-21引用 2楼sendDataToPairedDevice("text to send" ,bluetoothDevice); 这一行的bluetoothDevice我写到android studio里面以后是红色的,请问是什么原因。。
- BethanySeverance 2015-12-21引用 3楼
private void sendDataToPairedDevice(String message ,BluetoothDevice device){ byte[] toSend = message.getBytes(); try { UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(applicationUUID); OutputStream mmOutStream = socket.getOutputStream(); mmOutStream.write(toSend); // Your Data is sent to BT connected paired device ENJOY. } catch (IOException e) { Log.e(TAG, "Exception during write", e); } }
站点信息
- 文章2300
- 用户1336
- 访客10859399
每日一句
True success inspires others to act.
真正的成功是激励他人行动。
真正的成功是激励他人行动。
语法错误: 意外的令牌“标识符”
全面理解Gradle - 定义Task
Motrix全能下载工具 (支持 BT / 磁力链 / 百度网盘)
谷歌Pixel正在开始起飞?
获取ElementUI Table排序后的数据
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is
亲测!虚拟机VirtualBox安装MAC OS 10.12图文教程
华为手机app闪退重启界面清空log日志问题
android ndk开发之asm/page.h: not found
手机屏幕碎了怎么备份操作?
免ROOT实现模拟点击任意位置
新手必看修改DSDT教程
thinkpad t470p装黑苹果系统10.13.2
新会员