我应该使用哪个 UUID 来读取特定蓝牙设备的特征?

which UUID should I use to read Characteristic from specific Bluetooth device?

我应该使用哪个 UUID 来读取特定蓝牙设备的特性? 据我所知,它不需要配对,我必须连接到设备并接收数据

我的代码正确吗?

另外 mBluetoothGattService 为空!

如何从特定设备接收数据?

        if (action.equals(BluetoothDevice.ACTION_FOUND)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getName() != null && device.getName().equals(BluetoothName)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    Log.d(TAG, "onReceive: " + device.getName() + ": " + device.getAddress());
                    if (device.getUuids() != null) {
                        for (int i = 0; i <= device.getUuids().length; i++) {
                            Log.d(TAG, "onReceive: " + device.getUuids()[i].getUuid().toString());
                        }
                    }
                    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                    if (!mBluetoothAdapter.isEnabled()) {
                        return;
                    }
                    device.connectGatt(context, true, new BluetoothGattCallback() {
                        @Override
                        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                            super.onServicesDiscovered(gatt, status);
                            if (status == BluetoothGatt.GATT_SUCCESS) {
                                BluetoothGattService mBluetoothGattService = mBluetoothGatt.getService(BTMODULEUUID);
                                if (mBluetoothGattService != null) {
                                    Log.i(TAG, "Service characteristic UUID found : " + mBluetoothGattService.getUuid().toString());
                                } else {
                                    Log.i(TAG, "Service characteristic not found for UUID : " + BTMODULEUUID);
                                }
                            }
                        }

                        @Override
                        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                            if (newState == BluetoothGatt.STATE_CONNECTED) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                    gatt.requestMtu(512);
                                }
                            }
                            mBluetoothGatt = gatt;
                            Log.d(TAG, "discoverServices Size : " + mBluetoothGatt.getServices().size());
                            for (BluetoothGattService s : mBluetoothGatt.getServices()) {
                                Log.d(TAG, "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, "onReceive connectGatt.");
                            readCustomCharacteristic(mBluetoothAdapter);
                        }
                    });
                }
            }
        }

public void readCustomCharacteristic(BluetoothAdapter mBluetoothAdapter) {
    if (mBluetoothAdapter == null) {
        return;
    }
    if (mBluetoothGatt == null) {
        return;
    }
    // Is the service available on the device
    BluetoothGattService mCustomService = mBluetoothGatt.getService(convertUuidFromInteger(0x181D));// BTMODULEUUID not worked too
    if (mCustomService == null) {
        Log.w(TAG, "Custom BLE Service not found");
        return;
    }
    // Read the characteristic from the service
    BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(BTMODULEUUID);
    if (!mBluetoothGatt.readCharacteristic(mReadCharacteristic)) {
        Log.w(TAG, "Failed to read characteristic");
    }
}

LogCat shows :

D: discoverServices Size : 0

D: onReceive connectGatt.

W: Custom BLE Service not found

终于解决了

代码如下:

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.");
            }
        });
    }
}

Have a Look at : gatt.discoverServices()

Declaration Line is Significant

Do No Call it Twice