Making Android BLE (Bluetooth LE) pretty stable

I am writing a program to execute the following BLE message between two Android devices that support Android peripheral communication (in this case MOTOROLA MOTO E Generation 2) with a series: connect → communicate → disconnect and see if they can do this with good stability. The issue found in the test is also discussed.

The program first allows you to choose whether you want the device to be peripheral or central. On the central side, the program first scans the peripherals with the filter on the Service UUID:

ScanSettings.Builder ssb = new ScanSettings.Builder(); ssb.setReportDelay(0); ssb.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY); ScanSettings ss = ssb.build(); ScanFilter.Builder sfb = new ScanFilter.Builder(); sfb.setServiceUuid(BLEShared.SERVICE_UUID); LinkedList<ScanFilter> lsf = new LinkedList<ScanFilter>(); lsf.add(sfb.build()); BluetoothLeScanner leScanner = m_BluetoothAdapter.getBluetoothLeScanner(); if(leScanner != null) { leScanner.startScan(lsf, ss, blePeripheralScanner); isScanning = true; currentState = BLE_CENTRAL_STATE_SCANNING; } 

Then, when the peripheral device is scanned, the handler will call the next from the main thread.

 stopScan(); mGatt = result.getDevice().connectGatt(BLECentral.this, false, m_BLECentralGattCallBack); 

When a peripheral device is connected (another MOTO E is in effect), in onConnectionStageChange ()

the following is true:
  if(newState == BluetoothGatt.STATE_CONNECTED) { m_Handler.post(new Runnable(){ public void run() { gatt.discoverServices(); } }); } 

After discovering all services, the program performs the following actions:

Updating the descriptors for subscribing to notifications records some data and sends them to the periphery when receiving data sent from the center, the peripheral device notifies you of a change in value. After receiving notification of a change in data from the periphery, they will send data to the periphery. The recording and notification process will be performed 11 times. Then the central call to gatt.disconnect () will disconnect the connection.

The aforementioned process loops to verify stability.

With a normal connection, the above process can be completed within 1.7-2.5 seconds. Between each recording notification process takes about 0.1 s

During the test, the following problems are detected:

  • onConnectionStageChange () takes a long time to call device.connectGatt () for up to 30 seconds. If such a long wait occurs, the next onConnectionStageChange () is likely to be disconnected by ocassionally.
  • onConnectionStageChange () is called quickly after device.connectGatt (), but newState = STATE_DISCONNECTED from time to time
  • Between each write command, 0.5 seconds is required.
  • The process either drains or slows down at any stage.

There seems to be some bugs related to Android BLE glass. Therefore, I try to implement a watchdog if any process is not as fast as expected, the watchdog activates and deactivates the Bluetooth of the central device and turns it on again, thereby proactively stopping the waiting for a response from the bluetooth stack, which is expected to have some error values. As soon as Bluetooth turns on again, the central unit will start scanning peripherals and continue the above test.

I tried just closing Gatt while activating a watchdog timer, however after such a force to close Gatt, a serial BLE connection tends to fail. So it seems that the error tends to accumulate after each failure. Therefore, I resort to disconnecting and connecting the Bluetooth device BluetoothAdapter ... disable ()

Turning off Bluetooth and turning it on again is quite intrusive for some users, as they can use another Bluetooth device. My questions:

  • How can we improve the stability of the above test (is there something wrong with my code, or in any case, how can I use the BLE stack better)?

  • In the event of a failure, is it possible to reset the BLE stack or close only a sufficient amount of resources, and not completely use the BLE stack switch?

The eclipse project is placed under it, if you are interested in giving it a test or improving the project, download it and try it. https://drive.google.com/file/d/0B-w_C5ISF1UHRXUzd1FrUHpyV0k/view?usp=sharing

+5
source share

All Articles