版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/csdn_kike/article/details/69793362
声明:代码基于网上某个小工程改的,如果涉及侵权,请联系本人,立马删除。
曾经做过一个小玩意,尝试把两个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);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
new Thread(new Runnable()
{
public void run()
{
if (mBluetoothAdapter.isEnabled())
{
scanLeDevice(true);
mScanning = true;
}else
{
...
}
}
}).start();
...
2 扫描连接:
scanLeDevice--> mBluetoothAdapter.startLeScan(mLeScanCallback)-->mLeScanCallback:
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
if (!mDeviceContainer.isEmpty())
{
if(!isEquals(device))
{
mDeviceList.add(device);//添加到设备列表,原代码中少了这句,导致后连接的设备盖了前面的设备
connectBle(device);//添加gatt到列表
}
}else
{
mDeviceList.add(device);
connectBle(device);
}
}
});
}
};
private void connectBle(BluetoothDevice device) {
mDeviceContainer.add(device);
while (true) {
if (mBluetoothLeService != null) {
mBluetoothLeService.connect(device.getAddress()); //real connect
break;
} else {
...
}
}
}
public boolean connect(final String address) {
...
BluetoothGatt bluetoothGatt;
bluetoothGatt = device.connectGatt(this, false, mGattCallback);
if(checkGatt(bluetoothGatt)){
connectionQueue.add(bluetoothGatt);
}
...
return true;
}
经过以上处理,多个device才算是较完善地进入apk的管理队列中了。后续的处理基本都是要么以gatt为输入做处理,要么有设备名字,所以这时候基本就不
需要特别考虑是哪个设备了,比如findService(BluetoothGatt gatt)这样的,多个设备的管理在此之前处理,因为这时 输入已经是某个具体的gatt了。在我的小程序里,
有个显示不同设备的框,这里我根据devicelist的名字的区别在BroadcastReceiver()做了处理:
...
if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action))
{
String data = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
if(strName.contains("NO.1"))
{
display(str1);
}
else
{
display(str2);
}
...
搞法比较土,还好可以用,呵呵。
3 断开连接:
public void disconnect() {
if (mBluetoothAdapter == null || connectionQueue.isEmpty())
{
...
}
// mBluetoothGatt.disconnect();
for(BluetoothGatt bluetoothGatt:connectionQueue){
bluetoothGatt.disconnect();
}
}
如果涉及到close等处理,也要把所有队列的东西该关闭的都关了。
还不熟悉编辑代码和文字的混合体。。。
————————————————
版权声明:本文为CSDN博主「csdn_kike」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/csdn_kike/article/details/69793362