检查设备是否支持蓝牙 实现步骤:
判断是否有蓝牙设备。 是否开启蓝牙。 若没有开启则请求开启蓝牙。 操作需要权限 :
1 2 3 4 5 6 7 8 9 10 | 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( "本机没有蓝牙设备" ); } |
1 2 3 4 5 | Intent intentvisible= new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); intentvisible.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 400); //这个值么默认120秒,超过300秒将会被设置为300.可是在我的设备上是2400也行。API出错? this .startActivity(intentvisible); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //开始扫描 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); } } |
1 2 3 4 5 6 7 8 | Set<bluetoothdevice> device=ba.getBondedDevices(); if (device.size()>0) { for (BluetoothDevice bd:device) { System.out.println(bd.getAddress()+bd.getName()); } }</bluetoothdevice> |
发送字符串到一个配对的设备。 需要已知名和地址。
1 2 3 4 5 6 7 8 9 10 11 12 | 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); } } |
1 | sendDataToPairedDevice( "text to send" ,bluetoothDevice); |
启动一个service去监听是否有数据返回。一旦有数据返回就启动一个线程去处理数据 处理完数据,通过广播去通知UI。 服务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** *负责监听启动应用程序 后的接收数据 */ 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)); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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() { //处理数据 } } |
1 2 3 4 5 6 7 8 9 10 11 | 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楼123456789101112
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
- 访客10870407
每日一句
Care and diligence bring luck.
谨慎和勤奋,带来好运气。
谨慎和勤奋,带来好运气。
VIP电影免费观看
解决安卓运行错误Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug
准猿们添加简历7种最佳技能
java.nio.ByteBuffer缓冲区简介
C++实现远程下载EXE并执行
通过C ++生成RGBA图像
Linux查看进程及相关操作常用命令
Android模拟屏幕点击的基本原理
解决vue动态生成el-table-column按钮点击事件
VMware Workstation安装Centos7及常用命令行
Notepad++ 16进制编辑功能
快速入门-如何在Java上使用Redis
Android Studio 提示Session 'app':Error Installing APK
新会员