gpt4 book ai didi

java - 无法写入 BLE GattCharacteristic

转载 作者:搜寻专家 更新时间:2023-11-01 08:23:04 26 4
gpt4 key购买 nike

我正在尝试连接到 BLE 设备,然后保持连接状态(因为我必须每秒写入大约 1 次特性。虽然我可以成功连接到设备,正如能够看到此日志 System.out.println("dev connected.."); 所证明的那样,但每当我调用发送消息时都会出现异常。异常告诉我 finalGatt 是一个空对象引用。 (请参阅下面的代码和错误)。

public class BluetoothLeService2 extends Service {
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mLEScanner;
private ScanSettings settings;
private List<ScanFilter> filters;
private BluetoothGatt mGatt;
private BluetoothGatt finalGatt;
private Application app;

final BluetoothManager bluetoothManager;

BluetoothLeService2(Application app) throws NullPointerException {
this.app = app;
bluetoothManager =
(BluetoothManager) app.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();


mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build();
filters = new ArrayList<>();
scanLeDevice(true);
}



private void scanLeDevice(final boolean enable) {
if (enable) {
mLEScanner.startScan(filters, settings, mScanCallback);
} else {
mLEScanner.stopScan(mScanCallback);
}
}


private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice btDevice = result.getDevice();
try {
if(btDevice.getAddress().equals("mac")){
connectToDevice(btDevice);
scanLeDevice(false);
}
} catch (NullPointerException e) {
//NULL
}
}

};



private void connectToDevice(final BluetoothDevice device) {
new Thread(new Runnable() {
public void run() {
while(true) { //keep device connected
if (mGatt == null) {
mGatt = device.connectGatt(app, true, gattCallback);
scanLeDevice(false);
}

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}


public void sendMessage() {
try {
List<BluetoothGattService> bgsList = finalGatt.getServices();
BluetoothGattService mSVC = bgsList.get(3);
List<BluetoothGattCharacteristic> bgcList = mSVC.getCharacteristics();
BluetoothGattCharacteristic mCH = bgcList.get(0);
mCH.setValue(Base64.decode("sth".getBytes(), Base64.URL_SAFE));
if (bluetoothManager.getConnectionState(finalGatt.getDevice(), BluetoothGatt.GATT) == 0) {
connectToDevice(finalGatt.getDevice());
return;
}
} catch (Exception e) {
Log.wtf("connection", "dev not connected");
}
}
}

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
if(gatt.getDevice().getAddress().equals("mac")) {
gatt.discoverServices();
}
}

}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> services = gatt.getServices();
if (status == BluetoothGatt.GATT_SUCCESS) {
if(gatt.getDevice().getAddress().equals("mac")){
finalGatt = gatt;
connectToDevice(finalGatt.getDevice());
System.out.println("dev connected..");
scanLeDevice(false);
}
}
}

};

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

异常:

Attempt to invoke virtual method 'java.util.List android.bluetooth.BluetoothGatt.getServices()' on a null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.bluetooth.BluetoothGatt.getServices()' on a null object reference
at com.faebl.BluetoothLeService2.sendMessage(BluetoothLeService2.java:135)
at com.faebl.Activator.execute(Activator.java:56)
at com.faebl.Activator.doInBackground(Activator.java:42)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

我在另一个类中这样执行它:

bles = new BluetoothLeService2(app);
bles.sendMessage();

我哪里错了?也许我必须用另一种方式来做?

感谢您的帮助。

最佳答案

您没有显示何时以及如何调用 sendMessage 并在其中抛出异常。很可能你在调用 onServicesDiscovered 之前就这样做了,因为只有那里设置了 finalGatt 值。

关于java - 无法写入 BLE GattCharacteristic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48322022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com