获取 gattService 失败。 BluetoothGatt 返回但没有任何服务
我无法获取 BT 服务列表
mBluetoothGatt.getService 方法返回 null 因为 mDevice.connectGatt 方法返回没有服务的 BluetoothGatt(大小 0)
我确信我正在使用的 BT 设备工作正常
这是我的第一个任务在一份新工作中,尽快解决它对我来说非常重要,
我错过了一些东西,但不知道是什么......
非常感谢
public class TagService extends Service {
String TAG = "TagService";
public static boolean running;
public e_status status;
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBtAdapter;
private BluetoothLeScanner mScanner;
private BluetoothDevice mDevice;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mService;
enum e_status {
none, init, searchDevice, foundDevice, discoverServices, sendHeartBeat, finish
}
@Override
public void onCreate() {
super.onCreate();
runThread();
}
@Override
public void onDestroy() {
super.onDestroy();
running = false;
}
@SuppressLint("MissingPermission")
private void runThread() {
status = e_status.none;
running = true;
new Thread(() -> {
while (running && status != e_status.finish) {
int interval=4000;
try {
if (status == e_status.none) {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBtAdapter = mBluetoothManager.getAdapter();
setStatus(e_status.init);
}
if (status == e_status.init) {
mScanner = mBtAdapter.getBluetoothLeScanner();
ArrayList<ScanFilter> filters = new ArrayList<>();
ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
filters.add(tagFilter);
ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
setStatus(e_status.searchDevice);
mScanner.startScan(filters, settings, getScanCallback());
}
if (status == e_status.foundDevice) {
mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
if (mBluetoothGatt != null) {
setStatus(e_status.discoverServices);
mBluetoothGatt.discoverServices();
}
}
if (status == e_status.discoverServices) {
mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
if (mService != null) {
setStatus(e_status.sendHeartBeat);
//
// this is no happen!!!
//
// TODO
//BluetoothGattCharacteristic charac = mService.getCharacteristic(ConstParams.serviceUUID);
//mBluetoothGatt.writeCharacteristic(characteristic);
//mService.addCharacteristic(charac, value));
}
}
} catch (Exception ex) {
Log.e(TAG, "error" ,ex);
interval=30000;
}
sleep(interval);
}
}).start();
}
private ScanCallback getScanCallback() {
return new ScanCallback() {
@SuppressLint("MissingPermission")
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
setStatus(e_status.foundDevice);
mScanner.stopScan(this);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
setStatus(e_status.init);
}
};
}
private void setStatus(e_status newStatus) {
Log.i(TAG, "status: " + status + " -> " + newStatus);
status = newStatus;
}
private BluetoothGattCallback getGattCallback() {
return new BluetoothGattCallback() {
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(gatt, txPhy, rxPhy, status);
Log.i(TAG, "onPhyUpdate");
}
@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyRead(gatt, txPhy, rxPhy, status);
Log.i(TAG, "onPhyRead");
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
super.onConnectionStateChange(gatt, status1, newState);
Log.i(TAG, "onConnectionStateChange" + status + "," + newState);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG, "onServicesDiscovered");
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG, "onCharacteristicRead");
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG, "onCharacteristicWrite");
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG, "onCharacteristicChanged");
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG, "onDescriptorRead");
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG, "onDescriptorWrite");
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
Log.i(TAG, "onReliableWriteCompleted");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
Log.i(TAG, "onReadRemoteRssi");
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
Log.i(TAG, "onMtuChanged");
}
@Override
public void onServiceChanged(@NonNull BluetoothGatt gatt) {
super.onServiceChanged(gatt);
Log.i(TAG, "onServiceChanged");
}
};
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
I could not get the BT services list
The method mBluetoothGatt.getService return null Because the method mDevice.connectGatt return BluetoothGatt with no services (size 0)
I know for sure that the BT Device I am working with is working properly
This is my first task in a new job and it is very important for me to solve it as quickly as I can
I'm missing something but do not know what ...
Thanks a lot
public class TagService extends Service {
String TAG = "TagService";
public static boolean running;
public e_status status;
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBtAdapter;
private BluetoothLeScanner mScanner;
private BluetoothDevice mDevice;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mService;
enum e_status {
none, init, searchDevice, foundDevice, discoverServices, sendHeartBeat, finish
}
@Override
public void onCreate() {
super.onCreate();
runThread();
}
@Override
public void onDestroy() {
super.onDestroy();
running = false;
}
@SuppressLint("MissingPermission")
private void runThread() {
status = e_status.none;
running = true;
new Thread(() -> {
while (running && status != e_status.finish) {
int interval=4000;
try {
if (status == e_status.none) {
mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBtAdapter = mBluetoothManager.getAdapter();
setStatus(e_status.init);
}
if (status == e_status.init) {
mScanner = mBtAdapter.getBluetoothLeScanner();
ArrayList<ScanFilter> filters = new ArrayList<>();
ScanFilter tagFilter = new ScanFilter.Builder().setDeviceAddress(ConstParams.tagAddress).build();
filters.add(tagFilter);
ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
setStatus(e_status.searchDevice);
mScanner.startScan(filters, settings, getScanCallback());
}
if (status == e_status.foundDevice) {
mBluetoothGatt = mDevice.connectGatt(getApplicationContext(), false, getGattCallback());
if (mBluetoothGatt != null) {
setStatus(e_status.discoverServices);
mBluetoothGatt.discoverServices();
}
}
if (status == e_status.discoverServices) {
mService = mBluetoothGatt.getService(ConstParams.serviceUUID);
if (mService != null) {
setStatus(e_status.sendHeartBeat);
//
// this is no happen!!!
//
// TODO
//BluetoothGattCharacteristic charac = mService.getCharacteristic(ConstParams.serviceUUID);
//mBluetoothGatt.writeCharacteristic(characteristic);
//mService.addCharacteristic(charac, value));
}
}
} catch (Exception ex) {
Log.e(TAG, "error" ,ex);
interval=30000;
}
sleep(interval);
}
}).start();
}
private ScanCallback getScanCallback() {
return new ScanCallback() {
@SuppressLint("MissingPermission")
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
mDevice = mBtAdapter.getRemoteDevice(result.getDevice().getAddress());
setStatus(e_status.foundDevice);
mScanner.stopScan(this);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
setStatus(e_status.init);
}
};
}
private void setStatus(e_status newStatus) {
Log.i(TAG, "status: " + status + " -> " + newStatus);
status = newStatus;
}
private BluetoothGattCallback getGattCallback() {
return new BluetoothGattCallback() {
@Override
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyUpdate(gatt, txPhy, rxPhy, status);
Log.i(TAG, "onPhyUpdate");
}
@Override
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
super.onPhyRead(gatt, txPhy, rxPhy, status);
Log.i(TAG, "onPhyRead");
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status1, int newState) {
super.onConnectionStateChange(gatt, status1, newState);
Log.i(TAG, "onConnectionStateChange" + status + "," + newState);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
Log.i(TAG, "onServicesDiscovered");
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
Log.i(TAG, "onCharacteristicRead");
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i(TAG, "onCharacteristicWrite");
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Log.i(TAG, "onCharacteristicChanged");
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
Log.i(TAG, "onDescriptorRead");
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.i(TAG, "onDescriptorWrite");
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
Log.i(TAG, "onReliableWriteCompleted");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
Log.i(TAG, "onReadRemoteRssi");
}
@Override
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
Log.i(TAG, "onMtuChanged");
}
@Override
public void onServiceChanged(@NonNull BluetoothGatt gatt) {
super.onServiceChanged(gatt);
Log.i(TAG, "onServiceChanged");
}
};
}
private void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以...我解决了!
问题是我在服务处于连接模式之前调用了discoverServices()。
现在已经可以使用了,
附上修改后的代码,希望有人会喜欢
so... I solved it!!!
The problem was that I call discoverServices() before the service was in Connected mode.
Now it's working
Attach the revised code, hope someone will enjoys it