0

그의 코드입니다. 이 예제를 어딘가에 보았습니다. 그러나 그것은 효과가 없었습니다.GATT 서비스 : 청색 비콘의 배터리 잔량을 받으십시오.

java.lang.NullPointerException이 : 안드로이드 java.lang.String의 '가상 메서드를 호출하는 시도 콘크리트

private static final UUID Battery_Service_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb"); 
private static final UUID Battery_Level_UUID = UUID.fromString("00002a19-0000-1000-8000-00805f9b34fb"); 
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
private BluetoothGattService mBluetoothGatt; 

...

public void readCustomCharacteristic() { 

    if (mBluetoothAdapter == null || mBluetoothGatt == null) { 
     Log.w("Beacons", "BluetoothAdapter not initialized"); 
     return; 
    } 
    /*check if the service is available on the device*/ 
    BluetoothGattService mBatteryService = mBluetoothGatt; 
    if(mBatteryService == null){ 
     Log.w("Beacons", "Battery BLE Service not found"); 
     return; 
    } 
    /*get the read characteristic from the service*/ 
    BluetoothGattCharacteristic mReadCharacteristic = mBatteryService.getCharacteristic(Battery_Level_UUID); 


    if(mReadCharacteristic.getStringValue(0) == null){ 
     Log.w("Beacons", "Failed to read characteristic"); 
    } else { 
     Log.i("Beacons", "Battery Level: " + mReadCharacteristic.getValue()); 
    } 
} 

, 나는 NullPointerException이 얻을. null 객체 참조에 대한 bluetooth.BluetoothGattCharacteristic.getStringValue (int) '

아마도 완벽한 서버, 서비스 및 브로드 캐스트 리시버를 구현해야합니까? 도움을 주셔서 감사합니다.

+0

당신은 예외를 얻을. mBluetoothGatt가 초기화되는 방식의 코드를 보여줄 수 있습니까? – davidgyoung

+0

오, 잊어 버렸습니다. 다음과 같이 onCreate (Bundle ...) 함수에 있습니다. mBluetoothGatt = new BluetoothGattService (Battery_Service_UUID, 0); –

+0

GATT DB 구조를 보려면 nRF connect와 같은 일부 BLE 탐색기 응용 프로그램에서 응용 프로그램을 열어야합니다. 그렇게하면 어떤 특성과 서비스인지 알 수 있습니다. – Emil

답변

0

new BluetoothGattService(Battery_Service_UUID, 0);을 사용하여 BluetoothGattService을 간단히 구성하는 데는 효과가 없습니다. 해당 코드 사용은 GattService를 Android 앱 내부에서 호스팅하기 때문에 오류가 발생합니다.

안드로이드에 대한 외부 블루투스 장치에서 GATT 블루투스 특성을 읽으려면 먼저 비동기 API를 사용하여 여러 단계를 거쳐야합니다 : 대한

  1. 스캔 및 블루투스 장치를 발견 할 수 있습니다. (당신이 어떻게 든 바로 하나를 선택해야하므로, 더 이상 볼 수있을 수 있습니다.) 장치에

    mBluetoothAdapter.startLeScan(mLeScanCallback); 
    // the above will bring a callback to onLeScan(final BluetoothDevice device, int rssi, 
        byte[] scanRecord) 
    
  2. 연결

    device.connectGatt(mContext, false, mGattCallback); 
    // the above will cause a callback to onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 
    
  3. 발견 장치의 서비스

    gatt.discoverServices(); 
    // the above will bring a callback to onServicesDiscovered(BluetoothGatt gatt, int status) 
    
  4. 서비스에서 특성 읽기

    gatt.getCharacteristic(Battery_Level_UUID); 
    

마지막 단계의 코드 줄은 질문의 코드가 수행하려고 시도하는 것으로 null이됩니다. 이 작업을 수행하려면 처음 세 단계를 수행해야합니다.

더 많은 정보를 원하시면 여기를 참조하십시오 mBluetoothGatt이 Battery_Level_UUID의 특성이 없기 때문에 https://developer.android.com/guide/topics/connectivity/bluetooth-le.html

+0

거기에 코드 예제를 게시했습니다. 저를 도울 수 있기를 바랍니다. –