Използвам Nexus 9 в периферен режим. Създадох екземпляр на GATT сървър:
mGattServer = mBluetoothManager.openGattServer(this, mBluetoothGattServerCallback);
Създадох BluetoothGattService & BluetoothGattCharacteristic. Добавена услуга към GATT сървър и характеристика към услугата.
BluetoothGattService service =new BluetoothGattService(SERVICE_UUID,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic offsetCharacteristic =
new BluetoothGattCharacteristic(CHARACTERISTIC_NUM_UUID,
//Read+write permissions
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
service.addCharacteristic(offsetCharacteristic);
mGattServer.addService(service);
Екземпляр на BluetoothGattServerCallBack:
private BluetoothGattServerCallback mBluetoothGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
//super.onConnectionStateChange(device, status, newState);
Log.i(TAG, "onConnectionStateChange "
+ getStatusDescription(status) + " "
+ getStateDescription(newState));
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected..");
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected..");
}
}
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
//super.onServiceAdded(status, service);
Log.i(TAG, "onServiceAdded");
}
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
Log.i(TAG, "onCharacteristicWriteRequest");
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
Log.i(TAG, "onCharacteristicReadRequest");
}
@Override
public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
super.onDescriptorReadRequest(device, requestId, offset, descriptor);
Log.i(TAG, "onDescriptorReadRequest");
}
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
Log.i(TAG, "onDescriptorWriteRequest");
}
@Override
public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
super.onExecuteWrite(device, requestId, execute);
Log.i(TAG, "onExecuteWrite");
}
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
Log.i(TAG, "onNotificationSent");
}
};
Сега, когато се опитвам да свържа клиент на GATT към този сървър, методът onConnectionStateChange() на BluetoothGattServerCallback никога не се извиква.
Код от приложението от страна на клиента:
За да се свържете със сървър на GATT, работещ на Nexus 9
mConnectedGatt = device.connectGatt(this, false, mGattCallback);
Екземпляр на BluetoothGattCallback:
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected to server");
gatt.discoverServices();
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disconnected from server");
} else if (status != BluetoothGatt.GATT_SUCCESS) {
gatt.disconnect();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
}
};
Устройството (Nexus 9) е откриваемо и го получавам в onLeScan(). Но когато се опитвам да се свържа със сървър на GATT, работещ на Nexus 9, състоянието в onConnectionStateChange винаги е STATE_DISCONNECTED.
Опитах се да се свържа от някои приложения на трети страни като "B-BLE", "BLE Scanner", "Bluetooth 4.0 Explorer". Nexus 9 може да бъде открит в тези приложения, но когато се опитвам да се свържа със сървъра на GATT, състоянието в onConnectionStateChange() винаги е STATE_DISCONNECTED.
Всякакъв вид помощ ще бъде оценена.
Благодаря предварително.