声明:代码基于网上某个小工程改的,如果涉及侵权,请联系本人,立马删除。
曾经做过一个小玩意,尝试把两个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);