重新登錄bluetooth-lowenergy問題android:我仍在使用延遲。
概念:在每一個引發BluetoothGattCallback(例如連接,服務發現,寫入,讀取)的主要動作之後,都需要一個dealy。附:看看上BLE API level 19 sample for connectivity谷歌的例子來理解廣播應該如何被髮送,並得到一些大致的瞭解等等......
首先,scan(或scan)爲BluetoothDevices,填充connectionQueue與所需的設備和呼叫initConnection()。
看看下面的例子。現在
private Queue<BluetoothDevice> connectionQueue = new LinkedList<BluetoothDevice>();
public void initConnection(){
if(connectionThread == null){
connectionThread = new Thread(new Runnable() {
@Override
public void run() {
connectionLoop();
connectionThread.interrupt();
connectionThread = null;
}
});
connectionThread.start();
}
}
private void connectionLoop(){
while(!connectionQueue.isEmpty()){
connectionQueue.poll().connectGatt(context, false, bleInterface.mGattCallback);
try {
Thread.sleep(250);
} catch (InterruptedException e) {}
}
}
如果一切都很好,你已經建立的連接和BluetoothGattCallback.onConnectionStateChange(BluetoothGatt gatt, int status, int newState)被調用。
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch(status){
case BluetoothGatt.GATT_SUCCESS:
if (newState == BluetoothProfile.STATE_CONNECTED) {
broadcastUpdate(BluetoothConstants.ACTION_GATT_CONNECTED, gatt);
}else if(newState == BluetoothProfile.STATE_DISCONNECTED){
broadcastUpdate(BluetoothConstants.ACTION_GATT_DISCONNECTED, gatt);
}
break;
}
}
protected void broadcastUpdate(String action, BluetoothGatt gatt) {
final Intent intent = new Intent(action);
intent.putExtra(BluetoothConstants.EXTRA_MAC, gatt.getDevice().getAddress());
sendBroadcast(intent);
}
P.S.sendBroadcast(意向)可能需要這樣做:
Context context = activity.getBaseContext();
context.sendBroadcast(intent);
然後廣播由BroadcastReceiver.onReceive(...)
public BroadcastReceiver myUpdateReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(BluetoothConstants.ACTION_GATT_CONNECTED.equals(action)){
//Connection made, here you can make a decision: do you want to initiate service discovery.
// P.S. If you are working with multiple devices,
// make sure that you start the service discovery
// after all desired connections are made
}
....
}
}
收到後做任何你在廣播接收機想,這是我如何繼續:
private Queue<BluetoothGatt> serviceDiscoveryQueue = new LinkedList<BluetoothGatt>();
private void initServiceDiscovery(){
if(serviceDiscoveryThread == null){
serviceDiscoveryThread = new Thread(new Runnable() {
@Override
public void run() {
serviceDiscovery();
serviceDiscoveryThread.interrupt();
serviceDiscoveryThread = null;
}
});
serviceDiscoveryThread.start();
}
}
private void serviceDiscovery(){
while(!serviceDiscoveryQueue.isEmpty()){
serviceDiscoveryQueue.poll().discoverServices();
try {
Thread.sleep(250);
} catch (InterruptedException e){}
}
}
同樣,一個成功的服務後發現,BluetoothGattCallback.onServicesDiscovered(...)被調用。同樣,我向BroadcastReceiver發送一個意向(這次使用不同的動作字符串),現在您可以開始閱讀,編寫和啓用通知/指示... P.S.如果您正在使用多臺設備,請確保您在所有設備報告已發現其服務後開始讀取,寫入等等。
private Queue<BluetoothGattCharacteristic> characteristicReadQueue = new LinkedList<BluetoothGattCharacteristic>();
private void startThread(){
if(initialisationThread == null){
initialisationThread = new Thread(new Runnable() {
@Override
public void run() {
loopQueues();
initialisationThread.interrupt();
initialisationThread = null;
}
});
initialisationThread.start();
}
}
private void loopQueues() {
while(!characteristicReadQueue.isEmpty()){
readCharacteristic(characteristicReadQueue.poll());
try {
Thread.sleep(BluetoothConstants.DELAY);
} catch (InterruptedException e) {}
}
// A loop for starting indications and all other stuff goes here!
}
BluetoothGattCallback將有來自BLE傳感器所有的輸入數據。一個好的做法是將廣播與數據一起發送到您的BroadcastReceiver並在那裏處理。
您是否需要連接?如果您擔心隱藏數據和可靠或全部失敗,也許您可以簡單地將它放在廣播數據包中並進行掃描。 –
謝謝您的重播。我必須使用連接模式的可靠性原因 –
我有搜索遍佈網絡找到示例如何做你在你做的測試程序@Andreas Mueller。您能否如此善待我,並告訴我如何通過修改的「應用程序加速器」代碼將Android通知發送至CC2540芯片? (鏈接到您的項目可能?) – HenrikS