我正在尝试编写低功耗蓝牙 - AlertNotificationService(ANS)。这是一种有点奇怪的服务,因为通常服务在GAP外设上运行。但ANS在GAP中心运行。因此,典型的工作方式应为:
Watch - GAP peripheral (broadcasting), GATT client
Phone - GAP central , GATT server
基本上它对我有用但并不总是如此。这种不稳定对我来说很奇怪。当我看蓝牙LE分析仪时,我发现Android GATT服务器有时会告诉我的个人资料中没有特征......
看起来像这样:“看”请求我的GATT服务(我知道它是专有的而不是ANS UUID)
Slave->Master ATT Rcvd Find By Type Value Request, GATT Primary Service Declaration 11:22:33:44:11:22:33:44:11:76:62:65:01:00:00:00
Phone说服务从句柄0x35开始
Master->Slave ATT Rcvd Find By Type Value Response Handle: 0x0035
观察从句柄0x35
询问characteritiscSlave->Master ATT Rcvd Read By Type Request, GATT Characteristic Declaration, Handles: 0x0035..0xffff
但手机有时会错误地说这个手柄没有特色:
Master->Slave ATT Rcvd Error Response - Attribute Not Found, Handle: 0x0035
当我在GATT服务器中添加服务和特性时,我总是从函数中获得“true”。我是这样做的:
BluetoothGattService service =new BluetoothGattService(Vbe_AnsExt.UUID_SERVICE,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic characApp =
new BluetoothGattCharacteristic(Vbe_AnsExt.UUID_CharacApp,
BluetoothGattCharacteristic.PROPERTY_READ ,
BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattCharacteristic characMsg =
new BluetoothGattCharacteristic(Vbe_AnsExt.UUID_CharacMsg,
BluetoothGattCharacteristic.PROPERTY_READ ,
BluetoothGattCharacteristic.PERMISSION_READ );
boolean ret;
ret = service.addCharacteristic(characApp);
Log.i("vbeInit_ASN_Ext_Server","addCharApp retruned: "+ret);
ret = service.addCharacteristic(characMsg);
Log.i("vbeInit_ASN_Ext_Server","addCharMsg retruned: "+ret);
ret = mGattServer.addService(service);
Log.i("vbeInit_ASN_Ext_Server","addService retruned: "+ret);
知道可能是什么问题吗?我注意到有函数BluetoothGattServer::connect()
。我不确定如何使用它。我正在使用标准BluetoothDevice::connectGatt()
。但我想如果我做错了什么就永远不会 - 有时......
我在Samsung SM-G920F上使用Android M(6.0.1)。
[UPDATE]
我注意到手机重启后总能正常工作。应用程序关闭后重新打开它通常不起作用。它不能以不同的方式工作 - >
当我启动Gatt服务器时,我这样做:
mGattServer = bluetoothManager.openGattServer(appContext, mGattServerCallback);
mGattServer.clearServices();
当我关闭app(onDestroy()
)时,我会关闭gattserver:
mGattServer.close();
我也试图不关闭GATT服务器,但它没有帮助。任何想法在结束和重新开放之间可能出错?
答案 0 :(得分:0)
所以我可能找到了原因。我正在添加2个服务:ANS和我的服务ANS_extension。当我在回拨onServiceAdded()
所以现在它看起来像这样:
public static UUID UUID_SupportedNewAlertCategory = UUID.fromString("00002a47-0000-1000-8000-00805f9b34fb");
public static UUID UUID_NewAlert = UUID.fromString("00002a46-0000-1000-8000-00805f9b34fb");
// descriptor used for enabling notify feature
public static UUID UUID_Descr_new = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
mGattServer = bluetoothManager.openGattServer(appContext, mGattServerCallback);
mGattServer.clearServices();
waitServiceAdded = true;
BluetoothGattService service = new BluetoothGattService(Vbe_Ans.UUID_SERVICE,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic characSupportedNewAlerCategory =
new BluetoothGattCharacteristic(Vbe_Ans.UUID_SupportedNewAlertCategory,
BluetoothGattCharacteristic.PROPERTY_READ ,
BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattCharacteristic characNewAlert =
new BluetoothGattCharacteristic(Vbe_Ans.UUID_NewAlert,
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ );
BluetoothGattDescriptor bgd = new BluetoothGattDescriptor(UUID_Descr_new,
BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
characNewAlert.addDescriptor(bgd);
...
ret = service.addCharacteristic(characSupportedNewAlerCategory);
ret = service.addCharacteristic(characNewAlert);
...
ret = mGattServer.addService(service);
while(waitServiceAdded);
waitServiceAdded = true;
//init second service similar way as previous
while(waitServiceAdded);
然后我在GATT服务器回调中清除等待标志waitServiceAdded
。 (我是java新手所以你可能想用一些线程互斥同步来访问布尔值?)
private BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onServiceAdded (int status,
BluetoothGattService service){
waitServiceAdded = false;
}