Android之蓝牙开发初编(发现蓝牙,配对蓝牙,连接蓝牙)
来源:互联网 发布:萨尔 知乎 编辑:程序博客网 时间:2024/05/18 09:12
1.添加蓝牙权限:
<uses-permission android:name="android.permission.BLUETOOTH"/><uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2.搜索蓝牙(使用BluetoochAdapter)
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();//获取蓝牙if (adapter==null){ ToastUtil.showToast(context,"该设备不支持蓝牙"); return; }if (!adapter.isEnabled()){//是否打开蓝牙设备 Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); context.startActivityForResult(intent,REQUEST_ENABLE); Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); context.startActivity(discoverableIntent);}else { Set<BluetoothDevice> sets=adapter.getBondedDevices(); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); context.registerReceiver(mreceiver, filter); if (sets.size()>0){//获取已经配备设备 for (BluetoothDevice device:sets){ bluetoothDevices.add(device); bd=device; new ConnectThread().start(); } }else { adapter.startDiscovery(); }}
3.蓝牙广播:
private static BroadcastReceiver mreceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { bd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);//发现蓝牙设备 Log.d("xxx", "find:" + bd.getName()); if (bd.getName()!=null){ if (bd.getName().contains("BF4030")){//查找设备名为:BF4030进行配对 if (bd.getBondState()==BluetoothDevice.BOND_NONE){//如果没有配对 //进行配对 try { Method mm = bd.getClass().getMethod("createBond", (Class[]) null); mm.invoke(bd, (Object[]) null);//这里会弹出输入pin窗口 }catch (Exception e){ e.printStackTrace(); } }else if (bd.getBondState()==BluetoothDevice.BOND_BONDED){ new ConnectThread().start();//已经配对进行连接 } } } }else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){//配对状态的改变 bd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); switch (bd.getBondState()){ case BluetoothDevice.BOND_BONDING: LogUtil.ShowLog(TAG,"正在配对..."); break; case BluetoothDevice.BOND_BONDED: LogUtil.ShowLog(TAG,"完成配对."); new ConnectThread().start(); break; case BluetoothDevice.BOND_NONE: LogUtil.ShowLog(TAG,"取消配对."); break; } }else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){ LogUtil.ShowLog(TAG,"已经连接上蓝牙设备"); }else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)){ LogUtil.ShowLog(TAG,"已经断开蓝牙设备"); } } };
public static class ConnectThread extends Thread { static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB"; public ConnectThread() { BluetoothSocket tmp = null; try { tmp=bd.createRfcommSocketToServiceRecord(UUID.fromString(SPP_UUID)); } catch (IOException e) { e.printStackTrace(); } bluesocket = tmp; } @Override public void run() { adapter.cancelDiscovery();//取消蓝牙发现 try { if (!bluesocket.isConnected()){ bluesocket.connect();//发起连接 //连接成功后读取数据 handler.sendEmptyMessage(101);//连接成功 new ConnectedThread().start(); } } catch (IOException e) { handler.sendEmptyMessage(103);//断开连接 } } }
private static class ConnectedThread extends Thread { private final InputStream mis; private ConnectedThread() { InputStream tmpis = null; try { tmpis = bluesocket.getInputStream(); } catch (IOException e) { e.printStackTrace(); } mis = tmpis; } @Override public void run() { byte[] buffer = new byte[1024]; int bytes; while (true) { try { if (bluesocket.isConnected()){ bytes = mis.read(buffer);//读取得到的信息发送消息 handler.obtainMessage(102,bytes,-1,buffer).sendToTarget(); }else { handler.sendEmptyMessage(103);//断开连接 return; } } catch (IOException e) { handler.sendEmptyMessage(103);//断开连接 return; } } } }
阅读全文
1 0
- Android之蓝牙开发初编(发现蓝牙,配对蓝牙,连接蓝牙)
- android开发之蓝牙配对连接的方法
- android开发之蓝牙主动配对连接手机
- android开发之蓝牙配对连接的方法
- android开发之蓝牙主动配对连接手机
- android开发之蓝牙主动配对连接手机
- android开发之蓝牙配对连接的方法
- android开发之蓝牙配对连接的方法
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android开发之蓝牙连接与配对设备
- android开发之蓝牙配对连接的方法
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android开发蓝牙篇之蓝牙配对、连接与接收数据
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- iOS 设置圆角通用方法
- 使用 Office 365 PowerShell 管理用户帐户和许可证(二)使用 Office 365 PowerShell 查看授权和未授权的用户
- rex run 传参
- 38. Count and Say(第十二周)
- 海航集团王健出席海南省脱贫攻坚暨扶贫日主题晚会
- Android之蓝牙开发初编(发现蓝牙,配对蓝牙,连接蓝牙)
- 嵌入式Linux中tftp和nfs的笔记
- CoInitializeEx function
- Python: PS 滤镜--扇形变换
- Linux设备树的用法
- 查询行业数据报告
- SQL Server触发器及其操作语句
- 海航集团王健随同罗保铭书记出访加拿大和美国
- 双向循环型多链接链表