了解适用于Android的蓝牙低功耗特性

时间:2014-01-29 18:51:43

标签: android bluetooth bluetooth-lowenergy

我试图简单地从一个星系S3读取和编写Hello World到连接到虚拟串行端口的blueradio加密狗。但我得到了

Unhandled exception: java.lang.NullPointerException

什么时候打电话

gatt.readCharacteristic(characteristic);

我用它来定义特征

private static final UUID MY_UUID = UUID.fromString("00001801-0000-1000-8000-00805f9b34fb");
private static final UUID charUUID = UUID.fromString("00002a01-0000-1000-8000-00805f9b34fb");
characteristic = gatt.getService(MY_UUID).getCharacteristic(charUUID);

当我调用discoverServices()时,我从LogCat中获取的UUID

D/BluetoothGatt(7083): discoverServices() - device: EC:FE:7E:11:12:A4
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=00001800-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=00001801-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=0000180f-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetService() - Device=EC:FE:7E:11:12:A4 UUID=da2b84f1-6279-48de-bdc0-afbea0226079
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a00-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a01-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a02-0000-1000-8000-00805f9b34fb
D/BluetoothGatt(7083): onGetCharacteristic() - Device=EC:FE:7E:11:12:A4 UUID=00002a03-0000-1000-8000-00805f9b34fb

这就是我不确定我是否正确操作的地方我不知道如何为特征和服务获得正确的UUID

Beloiw是我的完整回调功能

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    public void testFunction(BluetoothGatt gatt){
        Log.d(TAG, "In Test Function");
        gatt.readRemoteRssi();
        BluetoothGattCharacteristic characteristic;
        characteristic = gatt.getService(MY_UUID).getCharacteristic(charUUID);
        characteristic.setValue("Hello World");
        gatt.readCharacteristic(characteristic);
    }
     @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status,
            int newState) {


            gatt.discoverServices();


    }


@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(TAG, "Services Discovered: "+ status);
    //mHandler.sendMessage(Message.obtain(null, MSG_PROGRESS, "Enabling Sensors..."));
    /*
     * With services discovered, we are going to reset our state machine and start
     * working through the sensors we need to enable
     */
            testFunction(gatt);
}


public void     onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
    Log.d(TAG, "Characteristic Changed: "+ characteristic.getValue());
}

public void     onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
    Log.d(TAG, "Characteristic Read: "+ status);
}

public void     onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
    Log.d(TAG, "Characteristic Write: "+ status);
}



public void     onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
    Log.d(TAG, "Descriptor Read: "+ status);
}

public void     onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
    Log.d(TAG, "Descriptor Write: "+ status);
}

public void     onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){
    Log.d(TAG, "Read Rssi: "+ status);
}

public void     onReliableWriteCompleted(BluetoothGatt gatt, int status){
    Log.d(TAG, "Reliable Write: "+ status);
}


};

2 个答案:

答案 0 :(得分:3)

UUID由bluetooth.org预定义,它定义了前缀,例如: 00001800,最后一部分" 0000-1000-8000-00805f9b34fb"对所有人来说都是一样的。

查看预定义的UUID here列表。

答案 1 :(得分:0)

我正在尝试使用Blueradio USB加密狗。 我在BlueRadio Android示例项目中找到了以下信息。可能此信息可能对您有用

BRSP服务的UUID

www.site.com/work/2

BRSP的特征UUID

DA2B84F1627948DEBDC0AFBEA0226079

如果您发现更多信息,请分享。

由于 约翰