使用 TinyB 的线程安全并发蓝牙LE

问题描述

有很多关于 Android BluetoothLE 编程的信息,但我正在设计一个使用 TinyB (github page,Java documentation) 的小型蓝牙应用程序,它具有与 Android 类似的 API BluetoothLE 库。

我是 Java 并发编程的新手,并且在这里和那里阅读了一些关于最佳实践的章节。不过,我不确定的是处理多个蓝牙连接的最佳做法。

为了连接设备,过程相当简单:

// Get the bluetooth manager and start discovery
BluetoothManager manager = BluetoothManager.getBluetoothManager().startdiscovery();



BluetoothDevice device = null ;
List<BluetoothDevice> list = manager.getDevices() ; 

for(Bluetooth device : list){
    // find your device based on what address you're looking for
}

// find the GATT service and characteristic you want to write to
BluetoothGattService tempService = getService(device,"xxxxx-xxxxx-xxxx-xxxx");
BluetoothGattCharacteristic tempValue = getCharacteristic(tempService,"xxxx-xxxx-xxxx-xxxx");

// write to the characteristic
byte[] config = {0x01} ;
tempValue.writeValue(config) ;

代码主要取自Intel-iot-devkit's hello TinyB

所以这非常适合一次连接到一台设备。对于多个连接,我想我可以有一个 ExecutorService 来运行创建连接或配对的任务,但是如果多个线程尝试同时使用 BluetoothManager 怎么办?这是允许的还是应该同步 BluetoothManager

编辑

没有阅读我引用的程序的评论,我感到很惭愧,但显然一次只能引用一个 BluetoothManager。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)