Android实现一个apk连接两个ble设备

声明:代码基于网上某个小工程改的,如果涉及侵权,请联系本人,立马删除。曾经做过一个小玩意,尝试把两个ble单片机设备都连在一个apk上,同时通信。网上可以找到类似的,但我找到的代码不够完整,还是自己折腾出来的,希望能帮到有需要的人。思想简单,一个BluetoothGatt结构管理一个device,两个BluetoothGatt放在一个队列里:private ArrayList conn
摘要由CSDN通过智能技术生成

声明:代码基于网上某个小工程改的,如果涉及侵权,请联系本人,立马删除。

曾经做过一个小玩意,尝试把两个ble单片机设备都连在一个apk上,同时通信。网上可以找到类似的,但我找到的代码不够完整,还是自己折腾出来的,希望能帮到有需要的人。

思想简单,一个BluetoothGatt结构管理一个device,两个BluetoothGatt放在一个队列里:

private ArrayList<BluetoothGatt> connectionQueue = new ArrayList<BluetoothGatt>();
	同时,另一个东西也要跟上来:设备列表,如果不设个list,则后面可能搞不清是哪个device,导致一个设备盖了另一个:
 
private ArrayList<BluetoothDevice> mDeviceList = new ArrayList<BluetoothDevice>();

代码片段按照流程表示如下:

1 老一套,初始化:
 
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show(); finish(); } // Initializes a Bluetooth adapter. For API level 18 and above, get a reference to  // BluetoothAdapter through BluetoothManager.  final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值