Ich habe derzeit eine Methode, die auf die BLE-Geräte schreibt, um es zu piepen. Mein Bluetooth Rückruf geht wie folgt:Bluetooth Gatt Callback funktioniert nicht mit neuer API für Lollipop
ReadCharacteristic rc = new ReadCharacteristic(context, ds.getMacAddress(), serviceUUID, UUID.fromString(myUUID), "") {
@Override
public void onRead() {
Log.w(TAG, "callDevice onRead");
try{Thread.sleep(1000);}catch(InterruptedException ex){}
WriteCharacteristic wc = new WriteCharacteristic(activity, context, getMacAddress(), serviceUUID, UUID.fromString(myUUID), ""){
@Override
public void onWrite(){
Log.w(TAG, "callDevice onWrite");
}
@Override
public void onError(){
Log.w(TAG, "callDevice onWrite-onError");
}
};
// Store data in writeBuffer
wc.writeCharacteristic(writeBuffer);
}
@Override
public void onError(){
Log.w(TAG, "callDevice onRead-onError");
}
};
rc.readCharacteristic();
Meine ReadCharacteristic Implementierung ist wie folgt:
public class ReadCharacteristic extends BluetoothGattCallback {
public ReadCharacteristic(Context context, String macAddress, UUID service, UUID characteristic, Object tag) {
mMacAddress = macAddress;
mService = service;
mCharacteristic = characteristic;
mTag = tag;
mContext = context;
this.activity =activity;
final BluetoothManager bluetoothManager =
(BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
final private static String TAG = "ReadCharacteristic";
private Object mTag;
private String mMacAddress;
private UUID mService;
private UUID mCharacteristic;
private byte[] mValue;
private Activity activity;
private BluetoothAdapter mBluetoothAdapter;
private Context mContext;
private int retry = 5;
public String getMacAddress() {
return mMacAddress;
}
public UUID getService() {
return mService;
}
public UUID getCharacteristic() {
return mCharacteristic;
}
public byte[] getValue() { return mValue; }
public void onRead() {
Log.w(TAG, "onRead: " + getDataHex(getValue()));
}
public void onError() {
Log.w(TAG, "onError");
}
public void readCharacteristic(){
if (retry == 0)
{
onError();
return;
}
retry--;
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(getMacAddress());
if (device != null) {
Log.w(TAG, "Starting Read [" + getService() + "|" + getCharacteristic() + "]");
final ReadCharacteristic rc = ReadCharacteristic.this;
device.connectGatt(mContext, false, rc);
}
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.w(TAG,"onConnectionStateChange [" + status + "|" + newState + "]");
if ((newState == 2)&&(status ==0)) {
gatt.discoverServices();
}
else{
Log.w(TAG, "[" + status + "]");
// gatt.disconnect();
gatt.close();
try
{
Thread.sleep(2000);
}
catch(Exception e)
{
}
readCharacteristic();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.w(TAG,"onServicesDiscovered [" + status + "]");
BluetoothGattService bgs = gatt.getService(getService());
if (bgs != null) {
BluetoothGattCharacteristic bgc = bgs.getCharacteristic(getCharacteristic());
gatt.readCharacteristic(bgc);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
Log.w(TAG,"onCharacteristicRead [" + status + "]");
if (status == BluetoothGatt.GATT_SUCCESS) {
mValue = characteristic.getValue();
Log.w(TAG,"onCharacteristicRead [" + mValue + "]");
gatt.disconnect();
gatt.close();
onRead();
}
else {
gatt.disconnect();
gatt.close();
}
}
}
Dieser Strom Methode funktioniert völlig in Ordnung für Geräte KitKat und unten. Aber wenn ich die gleiche Funktion auf Lollipop ausführe, piept es das Gerät ein paar Mal und hört dann auf zu arbeiten. Von nun an sagt es jedes Mal, wenn ich versuche, eine Verbindung herzustellen, dass das Gerät getrennt ist und gibt mir einen Fehlercode von 257 in der OnConnectionStateChanged-Methode.
ich auch diesen Fehler, wenn ich diese Methode aufrufen -
04-20 14:14:23.503 12329-12384/com.webble.xy W/BluetoothGatt﹕ Unhandled exception in callback
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothGattCallback.onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)' on a null object reference
at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:181)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
at android.os.Binder.execTransact(Binder.java:446)
Gibt es jemand, das gleiche Problem konfrontiert? Ich habe das Objekt nie als null erkannt, wenn ich das Debugging probiert habe.
Ich habe das gleiche Problem in meiner App auf> = 5.0. Nichts Neues auf deiner Seite? – Hrk
Ich habe gerade herausgefunden, dass dieser Fehler auftritt, wenn ich disconnect gefolgt von close anrufe, weil das onConnectionStateChange-Objekt vor dem Aufruf von disconnect auf null gesetzt wird. Dieser Fehler wird nicht angezeigt, wenn ich nur kurz anrufe. Aber das hat das Problem nicht gelöst. Ich versuche immer noch, mir den Kopf zu zerbrechen. Es beginnt wieder zu arbeiten, wenn ich Bluetooth neu starte, aber dieses Problem bleibt auch nach einer Weile bestehen. Bitte lassen Sie mich wissen, wenn Sie eine Lösung dafür finden. –