наконец, я решил это.
Код похож на:
final String action = intent.getAction();
if (action == null || action.isEmpty()) {
return;
}
// Log.d(TAG, "onReceive action : " + action);
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Log.d(TAG, "onReceive : " + device.getName() + ": " + device.getAddress());
if (DEVICE_NAME_FORCE_PLATE.equals(device.getName())) {
connectToForcePlate(context, device);
}
}
private void connectToForcePlate(Context context, BluetoothDevice device) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.d(TAG, "connectToForcePlate Device Name : " + device.getName() + " Device address : " + device.getAddress());
if (device.getUuids() != null) {
for (int i = 0; i <= device.getUuids().length; i++) {
Log.d(TAG, "connectToForcePlate Device UUID #" + i + " : " + device.getUuids()[i].getUuid().toString());
}
}
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
return;
}
device.connectGatt(context, false, new BluetoothGattCallback() {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG, "onServicesDiscovered Status : " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService mBluetoothGattService = mBluetoothGatt.getService(convertUuidFromInteger(0x181D));
if (mBluetoothGattService == null) {
Log.i(TAG, "onServicesDiscovered Service characteristic not found for UUID : " + SERVICE_UUID);
return;
}
Log.i(TAG, "onServicesDiscovered Service characteristic UUID found : " + mBluetoothGattService.getUuid().toString());
// streamBytes(device);
// read the characteristic from the service
BluetoothGattCharacteristic mBluetoothGattCharacteristic = mBluetoothGattService.getCharacteristic(CHARACTERISTIC_UUID);
if (!mBluetoothGatt.readCharacteristic(mBluetoothGattCharacteristic)) {
Log.w(TAG, "onServicesDiscovered Failed to read characteristic");
return;
}
Log.i(TAG, "onServicesDiscovered Succeed to read characteristic");
// Log.i(TAG, "onServicesDiscovered Byte : " + mReadCharacteristic.getValue().length);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.w(TAG, "onCharacteristicRead ******************************");
if (!gatt.readCharacteristic(characteristic)) {
Log.w(TAG, "onCharacteristicRead Failed to read characteristic");
return;
}
Log.i(TAG, "onCharacteristicRead Succeed to read characteristic");
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
Log.d(TAG, "onConnectionStateChange CONNECTED.");
boolean isDiscoverable = gatt.discoverServices(); // Essential to declare right Here
Log.w(TAG, "onConnectionStateChange --> Discover Services : " + isDiscoverable);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
gatt.requestMtu(512);
}
}
mBluetoothGatt = gatt;
Log.d(TAG, "onConnectionStateChange --> discoverServices Size : " + mBluetoothGatt.getServices().size());
for (BluetoothGattService s : mBluetoothGatt.getServices()) {
Log.d(TAG, "onConnectionStateChange --> discoverServices : found " + s.getUuid());
for (BluetoothGattCharacteristic c : s.getCharacteristics()) {
Log.d(TAG, "--> characteristic : " + c.getUuid() + ":" + String.format("%x", c.getInstanceId()));
}
}
super.onConnectionStateChange(gatt, status, newState);
Log.d(TAG, "onConnectionStateChange connectGatt.");
}
});
}
}
Посмотрите на: gatt.discoverServices()
Декларация имеет значение
Не называйте это дважды