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

本文介绍如何在Android应用中实现一个apk同时连接并通信两个BLE(蓝牙低功耗)设备。通过使用BluetoothGatt结构管理每个设备,并将它们存储在队列中进行操作,确保连接和通信的顺利进行。

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

曾经做过一个小玩意,尝试把两个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等处理,也要把所有队列的东西该关闭的都关了。

	还不熟悉编辑代码和文字的混合体。。。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值