我正在开发一款BLE应用程序,想要运行一个服务(每隔10秒从活动中调用一次)来执行以下任务:
1)Gets list of our products available to connect to (done, works)
2)For each available device:
2a)connect to device
2b)discover services
2c)read 5 characteristics in this fashion:
2c1)read characteristic
2c2)onCharacteristicRead parse data
2c3)when finished with data read next characteristic
2c4)repeat until all are read (this is done using a state var and switch statement)
2d)disconnect from device
2e)connect to next device
2f)repeat until all devices are read from
2g)stopSelf()
所以问题是...一切都很好,但只有在短时间内。我可以执行整个服务启动{mainActivity中的startService(...);}到完成{服务中的stopSelf();}6次。
第7次我会得到BluetoothGatt注册回调失败的错误。我不确定为什么前6次成功运行,而第7次失败。
请记住,我正在从主线程进行所有BLE调用,并且在日志记录器中已从多个位置确认。
这是我的代码概述:
SERVICE.JAVA
private Handler handler = new Handler();
private BluetoothGatt cGatt = null;
private int unitIndex = 0; // keep track of currently connected unit
private int state = 0; //used to keep track of which characteristic to read next
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "Service Started...");
//get ArrayList of units
if(units.size > 0)
handler.post(connectNextRunnable); //calls connectNextDevice()
else
stopSelf();
}
private Runnable discoverServices = new Runnable()
{
public void run()
{
cGatt.discoverServices();
}
}
private Runnable readNextValue = new Runnable()
{
public void run()
{
BluetoothGattCharacteristic c = null;
switch(state)
{
//set c to appropriate characteristic
default: // all characteristics read
unitIndex++;
handler.post(connectNextRunnable)
return
}
cGatt.readCharacteristic(c);
}
}
private void connectNextDevice()
{
if(unitIndex == 0)
store System.nanoTime in variable
if(unitIndex >= units.size) //finished will all units
stopSelf();
if(unitIndex < units.size)
cGatt.disconnect //if null
cGatt.connectGatt(this, false, gattCallback)
}
private BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
public void onConnectionStateChange()
{
handler.post(discoverServices);
}
public void onServicesDeiscovered()
{
handler.post(readNextValue);
}
public void onCharacteristicRead()
{
ParseData();
}
private void ParseData()
{
//do stuff with data
handler.post(readNextValue);
}
}
如我所说,所有BLE的操作都是通过一个处理程序从主线程调用的。服务成功地运行了6次,但第7次我遇到了“无法注册回调”的错误。
如果您认为相关,我可以提供更多的logcat信息。在原始帖子中,我没有提供它,因为我正在输出大量信息以验证数据接收等内容。
以下信息是我的服务从开始到结束的第7次运行的logcat信息。
08-15 12:00:10.746: I/PMIQ BTS(32027): Service Started...
08-15 12:00:10.746: I/PMIQ BTS(32027): Units: 1
08-15 12:00:10.746: D/AbsListView(32027): unregisterIRListener() is called
08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to next device...
08-15 12:00:10.766: I/PMIQ BTS(32027): Unit index = 0
08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to pmIQ-IQ130_D93A
08-15 12:00:10.766: I/System.out(32027): main
08-15 12:00:10.766: D/BluetoothGatt(32027): connect() - device: 00:1E:C0:19:D9:3A, auto: false
08-15 12:00:10.766: D/BluetoothGatt(32027): registerApp()
08-15 12:00:10.766: D/BluetoothGatt(32027): registerApp() - UUID=e9d10870-4b09-451c-a9fa-c6b5f3594a77
08-15 12:00:10.766: I/BluetoothGatt(32027): Client registered, waiting for callback
08-15 12:00:10.766: D/BluetoothGatt(32027): onClientRegistered() - status=133 clientIf=0
08-15 12:00:10.766: I/PMIQ BTS(32027): CONECTION STATE CHANGED...Binder_2
**08-15 12:00:10.766: E/BluetoothGatt(32027): Failed to register callback**
08-15 12:00:10.766: I/PMIQ BTS(32027): Could not connect to null ... 257
08-15 12:00:10.766: I/PMIQ BTS(32027): Connecting to next device...
08-15 12:00:10.766: I/PMIQ BTS(32027): Unit index = 1
08-15 12:00:10.766: I/PMIQ BTS(32027): ******************************
08-15 12:00:10.766: I/PMIQ BTS(32027): Start Time: 4360642409647
08-15 12:00:10.766: I/PMIQ BTS(32027): End Time: 4360648970925
08-15 12:00:10.766: I/PMIQ BTS(32027): Difference: 6561278
08-15 12:00:10.766: I/PMIQ BTS(32027): Time to complete: 6
08-15 12:00:10.766: I/PMIQ BTS(32027): ******************************
08-15 12:00:10.876: I/PMIQ BTS(32027): ...Service Destroyed
如果你看到了这里,谢谢!我找不到关于status=133的任何信息?!它只会在回调失败时出现。其他情况下都是status=0。
08-15 12:00:10.766: D/BluetoothGatt(32027): onClientRegistered() - status=133 clientIf=0
如果有人能回答这个问题,那将对我非常有帮助。或者如果有人能告诉我为什么它只运行了6次。任何洞见或直觉都可能有所帮助!
谢谢大家!