2014-01-29 71 views
3

我正试图简单地从Galaxy S3读取并写入Hello World到连接到虚拟串行端口的蓝牙适配器。但我正在逐渐了解蓝牙低功耗的Android特征

Unhandled exception: java.lang.NullPointerException 

当过我打电话

gatt.readCharacteristic(characteristic); 

我用这个定义,我从当我打电话discoverServices的logcat的接过特征

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); 

的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); 
} 


}; 
+0

你或许应该搞清楚正是为空开始,那么为什么。如果你的帖子在发生异常的那一行字面上是正确的,那么你的“gatt”对象在那里是空的,但是看起来很可能不是这个异常的精确行。 –

+0

我用gatt.readCharacteristic(特性)试了一下其他所有东西;注释掉了,我没有得到那个错误,所以它的要么是我的特性,要么是无效的gatt – justin018

+0

你可以通过日志记录(或者调试器)找出哪些*,然后从中找出*为什么*。尽管在技术上,如果这是为异常发生而指定的确切线(而不仅仅是其调用堆栈的一部分),那么只有gatt可能是空值。 –

回答

3

UUID是Bluetooth预定义。 org,它定义了前缀,例如00001800,最后一部分“0000-1000-8000-00805f9b34fb”全部相同。

查看预定义的UUID here的列表。

+0

链接已死,你的意思是https://www.bluetooth.com/specifications/gatt/characteristics? – pstanton

0

我试图用Blueradio USB加密狗来做同样的事情。 我在BlueRadio Android示例项目上找到了以下信息。也许是这个信息可能会为BRSP服务

DA2B84F1627948DEBDC0AFBEA0226079 

特性UUID是

你UUID有用BRSP

99564A02DC014D3CB04E3BB1EF0571B2 --> Info 

A87988B9694C479C900E95DFA6C00A24 --> Mode 

BF03260C72054C25AF4393B1C299D159 --> RX 

18CDA7844BD3437085BBBFED91EC86AF --> TX(Data sent to Client PC/ANDROID from BLE) 

0A1934F524B84F13984237BB167C6AFF --> CTS 

FDD6B4D3046D4330BDEC1FD0C90CB43B --> RTS 

如果您了解更多信息,请分享。

感谢 约翰