我试图将简单设备(AB Shutter3)连接到PC上的java应用程序,但在连接之前没有收到通知。试试看
BluetoothDevice currentDev = (BluetoothDevice) container.getDevice();
currentDev.setTrusted(true);
if(!currentDev.getConnected()) {
currentDev.connect();
}
logger.info(currentDev.getName());然后获取所有服务、描述符并尝试启用以下通知:
public void findServiceAndCharacteristic(BlueToothDeviceContainer container, String serviceUid){
for (BluetoothGattService service : container.getGattServices()) {
if(service.getUUID().equals(CUSTOM_SHUTTER_SERVICE_2.value) ||
service.getUUID().equals(CUSTOM_SHUTTER_SERVICE.value) ||
service.getUUID().equals(GENERAL_DEVICE_INFORMATION.value)){
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for(BluetoothGattCharacteristic ch : characteristics){
logger.info(ch.getUUID());
try {
List<BluetoothGattDescriptor> descrs = ch.getDescriptors();
for(BluetoothGattDescriptor ds : descrs){
ds.enableValueNotifications(new BluetoothNotification<byte[]>() {
@Override
public void run(byte[] bytes) {
logger.info(ds.getUUID() + "getted");
logger.info("BUTTON WAS PUSHED!!!");
}
});
}
} catch (BluetoothException ex){
logger.warn(ch.getUUID() + " does not work.");
}
try{
BluetoothNotification btnm = new BluetoothNotification<byte[]>() {
@Override
public void run(byte[] bytes) {
logger.info(ch.getUUID() + "getted CH");
logger.info("BUTTON WAS PUSHED!!!");
}
};
ch.enableValueNotifications(btnm);
} catch (BluetoothException ex){
logger.warn(ch.getUUID() + " does not work.");
}
}但是run方法不起作用。我做错什么了,有人知道吗?
发布于 2022-10-10 10:05:29
问题不在图书馆。HID特性的问题。在设备上没有找到它,所以没有从它发出任何通知。
https://stackoverflow.com/questions/73323189
复制相似问题