简体   繁体   English

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

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

which UUID should I use to read Characteristic from specific Bluetooth device? 我应该使用哪个UUID从特定的蓝牙设备读取特征? As far as I know, It doesn't need pairing, I have to connect to the device and receive data 据我所知,它不需要配对,我必须连接到设备并接收数据

Is my code correct? 我的代码正确吗?

Also mBluetoothGattService is null ! 另外, mBluetoothGattService为null!

How I can receive data from the specific device? 如何从特定设备接收数据?

        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 : LogCat显示:

D: discoverServices Size : 0 D:discoverServices大小:0

D: onReceive connectGatt. D:onReceive connectGatt。

W: Custom BLE Service not found W:找不到自定义BLE服务

设备说明

finally, I solved it. 终于,我解决了。

Code is like: 代码就像:

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() 看看: gatt.discoverServices()

Declaration Line is Significant 声明线很重要

Do No Call it Twice 两次都不叫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM