2017-04-18 134 views
4

根據列出的服務here,我試圖訂閱來自Android設備的服務,但無法讓請求部分工作。試圖使用7905F431-B5CE-4E99-A40F-4B1E122D00D0做廣告,但外圍設備未顯示在iPhone的藍牙設備列表中。還使用(LE)掃描和過濾器發現ANCS服務器,但沒有運氣。任何提示?在Android上訂閱ANCS

編輯:代碼

BleService.java

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, 
              boolean enabled) { 
    Log.d(TAG, "setCharacteristicNotification"); 
    if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
     Log.w(TAG, "BluetoothAdapter not initialized"); 
     return; 
    } 
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.DESCRIPTOR)); 
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
    mBluetoothGatt.writeDescriptor(descriptor) 
} 

@Override 
public void onServicesDiscovered(BluetoothGatt gatt, int status) { 
    if (status == BluetoothGatt.GATT_SUCCESS) { 
     broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); 
    } 
} 

SampleGattAttributes.java

public class SampleGattAttributes { 
    public static String ANCS_SERVICE = "7905F431-B5CE-4E99-A40F-4B1E122D00D0"; 
    public static String NOTIFICATION_SOURCE = "9FBF120D-6301-42D9-8C58-25E699A21DBD"; 
    public static String DATA_CONTROL = "22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB"; 
    public static String DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb"; 
} 

MainActivity.java

private void displayServices(List<BluetoothGattService> services) { 
    for (BluetoothGattService service : services) { 
     if (service.getUuid().compareTo(UUID.fromString(ANCS_SERVICE)) == 0) { 
      for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) { 
       Log.d(TAG, "charac: " + characteristic.getUuid()); 
       for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) { 
        Log.d(TAG, "desc: " + descriptor.getUuid()); 
       } 
       if (characteristic.getUuid().compareTo(UUID.fromString(NOTIFICATION_SOURCE)) == 0) { 
        bleService.setCharacteristicNotification(characteristic, true); 
       } 
      } 
     } 
    } 
} 

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 
     if (BleService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { 
      displayServices(bleService.getSupportedGattServices()); 
     } 
    } 
}; 

更新:我是可以在連接到iPhone上宣傳的服務後找到服務和特徵。但是,在iPhone上收到通知後,訂閱通知不會觸發onCharacteristicChanged

update2:全部寫入descriptor.setValue調用成功,並按順序運行。

UPDATE3:測試設備:從this sample.

UPDATE4代碼的用過那一部分的Nexus 5X的Android 6.0.1; iPhone 6S + iOS版10.3.1

update5:BT記錄

寫請求 enter image description here

寫響應 enter image description here

+1

well在Nexus設備上在開發人員選項上,有一個複選框爲「啓用Bluetooth HCI snoop log」。當您啓用它時,設備將記錄藍牙堆棧上發生的所有事情....查看存儲在名爲「HCIsnoop.log」的Sd卡中的日誌。當您的文件放在HD上時,請下載此免費軟件:http://www.fte.com/support/download.aspx?demo=ComProbe%20SD&type=capture&iid=1v查看這些BT日誌文件。有了這個,你可以看到任何已經從一臺設備交換到另一臺設備的數據包(比如BT的Wireshark)。 – PN10

+0

任何你可以在Github上發佈這個應用程序的方式?一個簡單的ANCS安卓客戶端將是驚人的 – Jason

回答

3

哦,我有一個類似的問題,請確保您在iOS上測試時不使用本地通知,因爲這些通知不會通過ANCS傳播。無論出於何種原因,您都需要實時推送通知

0

我最近遇到了類似的問題。我最終做的解決這個問題的方法是在我的BluetoothGatt上添加一個writeDescriptor。我setCharacteristicNotification現在看起來是這樣的:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { 
if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
    Log.w(TAG, "BluetoothAdapter not initialized"); 
    return; 
} 
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); 

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); 
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 
mBluetoothGatt.writeDescriptor(descriptor); 
} 

或許,如果你可以發佈/鏈接代碼,我將能夠進一步診斷您的具體問題,但上面的代碼是解決我的問題。

+0

添加了一些代碼片段。 – mbmc