그의 코드입니다. 이 예제를 어딘가에 보았습니다. 그러나 그것은 효과가 없었습니다.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) '
아마도 완벽한 서버, 서비스 및 브로드 캐스트 리시버를 구현해야합니까? 도움을 주셔서 감사합니다.
당신은 예외를 얻을. mBluetoothGatt가 초기화되는 방식의 코드를 보여줄 수 있습니까? – davidgyoung
오, 잊어 버렸습니다. 다음과 같이 onCreate (Bundle ...) 함수에 있습니다. mBluetoothGatt = new BluetoothGattService (Battery_Service_UUID, 0); –
GATT DB 구조를 보려면 nRF connect와 같은 일부 BLE 탐색기 응용 프로그램에서 응용 프로그램을 열어야합니다. 그렇게하면 어떤 특성과 서비스인지 알 수 있습니다. – Emil