Gluon Mobile:低功耗蓝牙,如何在扫描后连接到具有给定 mac 地址的设备?
Gluon Mobile : Bluetooth Low Energy, how to connect to a device with a given mac address after scanning?
我在连接到 arduino nano sense 33 BLE 时遇到问题。 arduino 模块包含一个具有 UUID 的配置文件。
是否可以从 mac 地址连接到 arduino 以获取配置文件 UUID 然后是特征并最终读取已建立的特征?
我是这样处理的:
BleDevice device = new BleDevice();
device.setAddress("E4:38:4F:DA:9F:94"); // MAC ADDRESS of arduino
BleService bleService = BleService.create().get();
bleService.connect(device); // Connect to device
List<BleProfile> list_of_profiles = device.getProfiles(); // Get list of profiles
for(BleProfile profile : list_of_profiles){
System.out.println(profile.getUuid());// display uuid of profiles
//Then get characteristics from profile
//Then read characteristics
}
编辑:
扫描功能已更新:这就是我扫描设备的方式:
public void scan_4_devices(){
long t= System.currentTimeMillis();
long end = t+5000;
System.out.println("searching for devices ...");
BleService.create().ifPresent(ble -> {
ObservableList<BleDevice> ble_list_device = ble.startScanningDevices();
System.out.println("SIZE BEFORE while loop : "+ble_list_device.size());
ble_list_device.addListener((ListChangeListener<BleDevice>) c -> {
while (c.next() && System.currentTimeMillis() < end ) {
System.out.println("SIZE IN while loop : "+ble_list_device.size());
if (c.wasAdded()) {
for (BleDevice device : c.getAddedSubList()) {
System.out.println("Device found: " + device.getName());
}
}
}
ble.stopScanningDevices();
});
});
}
我想做的是设备搜索 5 秒,如果超过时间,我将停止设备搜索。但是应用程序一直崩溃,这是崩溃后的堆栈跟踪。
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): Activity, get touch event, pcount = 1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, pass to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, passed to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Pushing TouchState[1,TouchState.Point[id=0,x=688,y=28]] to TouchPipeline[SmallMove]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Applying SmallMove to TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set MouseState[x=688,y=28,wheel=0,buttonsPressed=IntSet[212]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): PPSRenderer: scenario.effect - createShader: Blend_SRC_IN
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): Activity, get touch event, pcount = 1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, pass to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, passed to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): Activity, get touch event, pcount = 1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, pass to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): don't add points, primary = -1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, passed to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Pushing TouchState[1,TouchState.Point[id=0,x=688,y=28]] to TouchPipeline[SmallMove]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Applying SmallMove to TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Pushing TouchState[0] to TouchPipeline[SmallMove]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Applying SmallMove to TouchState[0]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set TouchState[0]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set MouseState[x=688,y=28,wheel=0,buttonsPressed=IntSet[]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): searching for devices ...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] I/GluonAttach(27191): JNI_OnLoad_ble called
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): [BLESERVICE] Initializing native BLE from OnLoad
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): ATTACH_DALVIK, tid = 27218, existed? 0, dalvikEnv at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Util :: Load className com/gluonhq/helloandroid/DalvikBleService
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): ATTACH_DALVIK, tid = 27218, existed? 1, dalvikEnv at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GluonAttach(27191): DalvikBle, init
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GluonAttach(27191): Util <init>
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Calling Verify Permissions from Attach::Util
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): PermissionRequestActivity::Calling verifyPermissions
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): All requested permissions are granted
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Verify Permissions from native Attach::Util done
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): DETACH_DALVIK, tid = 27218, existed = 1, env at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Initializing native Ble done
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): ATTACH_DALVIK, tid = 27218, existed? 1, dalvikEnv at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GluonAttach(27191): BLE startScanningPeripherals
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): DETACH_DALVIK, tid = 27218, existed = 1, env at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): SIZE BEFORE while loop : 0
[mar. mars 30 10:06:21 CEST 2021][INFO] [SUB] --------- beginning of crash
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): ANR in com.hacare.ehacarebox (com.hacare.ehacarebox/com.gluonhq.helloandroid.MainActivity)
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): PID: 27191
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 34. Wait queue head age: 27397.5ms.)
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): Load: 7.93 / 7.95 / 7.83
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): CPU usage from 126795ms to 0ms ago (2021-03-30 10:04:15.526 to 2021-03-30 10:06:22.321):
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 11% 2867/system_server: 4% user + 7% kernel / faults: 21744 minor 78 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 4% 707/android.hardware.sensors@1.0-service: 1.3% user + 2.6% kernel / faults: 21 minor 5 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 2.4% 623/logd: 0.7% user + 1.7% kernel / faults: 539 minor 1 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.8% 23194/kworker/u16:11: 0% user + 1.8% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 2.1% 12638/com.android.bluetooth: 1.2% user + 0.9% kernel / faults: 2319 minor 6 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 2% 3318/com.android.systemui: 1.5% user + 0.5% kernel / faults: 7928 minor 376 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.7% 26869/kworker/u16:3: 0% user + 1.7% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.6% 23173/kworker/u16:9: 0% user + 1.6% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.3% 23196/kworker/u16:14: 0% user + 1.3% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.6% 24530/kworker/u16:0: 0% user + 0.6% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.9% 10811/com.sec.phone: 0.5% user + 0.3% kernel / faults: 171 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.8% 20538/kworker/u16:4: 0% user + 0.8% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.8% 732/surfaceflinger: 0.5% user + 0.3% kernel / faults: 308 minor 16 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.6% 702/android.hardware.graphics.composer@2.1-service: 0.2% user + 0.3% kernel / faults: 96 minor 2 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.5% 695/android.hardware.bluetooth@1.0-service-qti: 0.1% user + 0.3% kernel / faults: 15 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.4% 401/cfinteractive: 0% user + 0.4% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.3% 20184/com.google.android.googlequicksearchbox:search: 0.3% user + 0% kernel / faults: 3639 minor 16 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.3% 3/ksoftirqd/0: 0% user + 0.3% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.3% 80/smem_native_rpm: 0% user + 0.3% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.2% 13990/logcat: 0% user + 0.1% kernel / faults: 29 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 1//init: 0.1% user + 0% kernel / faults: 221 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 23078/mdss_fb0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 29022/com.google.android.gms.persistent: 0.1% user + 0% kernel / faults: 543 minor 1 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 15/ksoftirqd/1: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 29/ksoftirqd/3: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 123/kswapd0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 710/android.hardware.wifi@1.0-service: 0% user + 0.1% kernel / faults: 14 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 273/kgsl_worker_thr: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 25576/kworker/0:1: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 7/rcu_preempt: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 3096/cds_mc_thread: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 89/kcompactd0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 26744/kworker/1:0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 3307/com.sec.android.inputmethod: 0% user + 0% kernel / faults: 134 minor 5 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 3927/iod: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 10/rcuop/0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 95/system: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 624/servicemanager: 0% user + 0% kernel / faults: 4 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 26753/kworker/3:2: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 22/ksoftirqd/2: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 348/irq/305-fts_tou: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 25/rcuop/2: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 31420/adbd: 0% user + 0% kernel / faults: 642 minor 2 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 701/android.hardware.graphics.allocator@2.0-service: 0% user + 0% kernel / faults: 57 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 717/healthd: 0% user + 0% kernel / faults: 7 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 4187/com.sec.android.app.launcher: 0% user + 0% kernel / faults: 2727 minor 3 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 191/msm_serial_hs_0: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 2660/cnss-daemon: 0% user + 0% kernel / faults: 33 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 20087/com.google.android.gms: 0% user + 0% kernel / faults: 875 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 910/wlan_logging_th: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 27044/kworker/2:4: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 23224/com.android.vending: 0% user + 0% kernel / faults: 910 minor 3 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 18/rcuop/1: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 407/irq/181-spdm_bw: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 705/android.hardware.memtrack@1.0-service: 0% user + 0% kernel / faults: 12 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 5231/com.samsung.cmh:CMH: 0% user + 0% kernel / faults: 207 minor 3 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 26472/com.google.android.apps.docs: 0% user + 0% kernel / faults: 57 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 79/dsps_smem_glink: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 83/msm_wa
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] W/ActivityManager( 2867): anr : com.hacare.ehacarebox,0
如您所见,列表ObservableList<BleDevice> ble_list_device = ble.startScanningDevices();
的大小为0。这可能会导致应用程序崩溃
这是主要的class源代码:
package com.hacare;
import com.gluonhq.attach.util.Constants;
import com.hacare.views.PrimaryView;
import com.hacare.views.SecondaryView;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.visual.Swatch;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends MobileApplication {
public static final String PRIMARY_VIEW = HOME_VIEW;
public static final String SECONDARY_VIEW = "test";
@Override
public void init() {
addViewFactory(PRIMARY_VIEW, () -> new PrimaryView().getView());
addViewFactory(SECONDARY_VIEW, () -> new SecondaryView().getView());
DrawerManager.buildDrawer(this);
}
@Override
public void postInit(Scene scene) {
Swatch.BLUE.assignTo(scene);
scene.getStylesheets().add(Main.class.getResource("style.css").toExternalForm());
((Stage) scene.getWindow()).getIcons().add(new Image(Main.class.getResourceAsStream("/icon.png")));
}
public static void main(String[] args) {
System.setProperty(Constants.ATTACH_DEBUG,"true");
launch(args);
}
}
理想情况下,连接后您应该等待连接状态,然后再开始询问配置文件列表。
这些是您可以使用的一些代码片段:
设备发现
BleService.create().ifPresent(ble -> {
ObservableList<BleDevice> devices = ble.startScanningDevices();
...
ble.stopScanningDevices();
});
设备连接
BleService.create().ifPresent(ble -> {
device.stateProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
if (State.STATE_CONNECTED.equals(device.getState())) {
// device connected, get profiles:
ObservableList<BleProfile> profiles = device.getProfiles();
...
device.stateProperty().removeListener(this);
}
}
});
ble.connect(device);
});
个人资料特征
ObservableList<BleCharacteristic> characteristics = profile.getCharacteristics();
...
// read characteristic
BleService.create().ifPresent(ble ->
ble.readCharacteristic(device, profile.getUuid(), characteristic.getUuid());
// subscribe characteristic
BleService.create().ifPresent(ble ->
ble.subscribeCharacteristic(device, profile.getUuid(), characteristic.getUuid());
// write characteristic
BleService.create().ifPresent(ble ->
ble.writeCharacteristic(device, profile.getUuid(), characteristic.getUuid(), bytes));
...
Android BLE Framework 中发生了一些变化,目前解决此问题的方法是首先将 pom.xml 文件中的附加版本更改为 4.0.12-SNAPSHOT。
<attach.version>4.0.12-SNAPSHOT</attach.version>
然后添加以下存储库:
<repository>
<id>Snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
我在连接到 arduino nano sense 33 BLE 时遇到问题。 arduino 模块包含一个具有 UUID 的配置文件。
是否可以从 mac 地址连接到 arduino 以获取配置文件 UUID 然后是特征并最终读取已建立的特征?
我是这样处理的:
BleDevice device = new BleDevice();
device.setAddress("E4:38:4F:DA:9F:94"); // MAC ADDRESS of arduino
BleService bleService = BleService.create().get();
bleService.connect(device); // Connect to device
List<BleProfile> list_of_profiles = device.getProfiles(); // Get list of profiles
for(BleProfile profile : list_of_profiles){
System.out.println(profile.getUuid());// display uuid of profiles
//Then get characteristics from profile
//Then read characteristics
}
编辑: 扫描功能已更新:这就是我扫描设备的方式:
public void scan_4_devices(){
long t= System.currentTimeMillis();
long end = t+5000;
System.out.println("searching for devices ...");
BleService.create().ifPresent(ble -> {
ObservableList<BleDevice> ble_list_device = ble.startScanningDevices();
System.out.println("SIZE BEFORE while loop : "+ble_list_device.size());
ble_list_device.addListener((ListChangeListener<BleDevice>) c -> {
while (c.next() && System.currentTimeMillis() < end ) {
System.out.println("SIZE IN while loop : "+ble_list_device.size());
if (c.wasAdded()) {
for (BleDevice device : c.getAddedSubList()) {
System.out.println("Device found: " + device.getName());
}
}
}
ble.stopScanningDevices();
});
});
}
我想做的是设备搜索 5 秒,如果超过时间,我将停止设备搜索。但是应用程序一直崩溃,这是崩溃后的堆栈跟踪。
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): Activity, get touch event, pcount = 1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, pass to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, passed to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Pushing TouchState[1,TouchState.Point[id=0,x=688,y=28]] to TouchPipeline[SmallMove]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Applying SmallMove to TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set MouseState[x=688,y=28,wheel=0,buttonsPressed=IntSet[212]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): PPSRenderer: scenario.effect - createShader: Blend_SRC_IN
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): Activity, get touch event, pcount = 1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, pass to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, passed to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): Activity, get touch event, pcount = 1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, pass to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): don't add points, primary = -1
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] E/GraalGluon(27191): Native Dalvik layer got touch event, passed to native Graal layer...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Pushing TouchState[1,TouchState.Point[id=0,x=688,y=28]] to TouchPipeline[SmallMove]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Applying SmallMove to TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set TouchState[1,TouchState.Point[id=0,x=688,y=28]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Pushing TouchState[0] to TouchPipeline[SmallMove]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Applying SmallMove to TouchState[0]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set TouchState[0]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): traceEvent: Set MouseState[x=688,y=28,wheel=0,buttonsPressed=IntSet[]]
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): searching for devices ...
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] I/GluonAttach(27191): JNI_OnLoad_ble called
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): [BLESERVICE] Initializing native BLE from OnLoad
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): ATTACH_DALVIK, tid = 27218, existed? 0, dalvikEnv at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Util :: Load className com/gluonhq/helloandroid/DalvikBleService
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): ATTACH_DALVIK, tid = 27218, existed? 1, dalvikEnv at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GluonAttach(27191): DalvikBle, init
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GluonAttach(27191): Util <init>
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Calling Verify Permissions from Attach::Util
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): PermissionRequestActivity::Calling verifyPermissions
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GraalActivity(27191): All requested permissions are granted
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Verify Permissions from native Attach::Util done
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): DETACH_DALVIK, tid = 27218, existed = 1, env at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GluonAttach(27191): Initializing native Ble done
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): ATTACH_DALVIK, tid = 27218, existed? 1, dalvikEnv at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] V/GluonAttach(27191): BLE startScanningPeripherals
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalGluon(27191): DETACH_DALVIK, tid = 27218, existed = 1, env at 0x7db4883200
[mar. mars 30 10:05:13 CEST 2021][INFO] [SUB] D/GraalCompiled(27191): SIZE BEFORE while loop : 0
[mar. mars 30 10:06:21 CEST 2021][INFO] [SUB] --------- beginning of crash
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): ANR in com.hacare.ehacarebox (com.hacare.ehacarebox/com.gluonhq.helloandroid.MainActivity)
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): PID: 27191
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 34. Wait queue head age: 27397.5ms.)
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): Load: 7.93 / 7.95 / 7.83
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): CPU usage from 126795ms to 0ms ago (2021-03-30 10:04:15.526 to 2021-03-30 10:06:22.321):
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 11% 2867/system_server: 4% user + 7% kernel / faults: 21744 minor 78 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 4% 707/android.hardware.sensors@1.0-service: 1.3% user + 2.6% kernel / faults: 21 minor 5 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 2.4% 623/logd: 0.7% user + 1.7% kernel / faults: 539 minor 1 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.8% 23194/kworker/u16:11: 0% user + 1.8% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 2.1% 12638/com.android.bluetooth: 1.2% user + 0.9% kernel / faults: 2319 minor 6 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 2% 3318/com.android.systemui: 1.5% user + 0.5% kernel / faults: 7928 minor 376 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.7% 26869/kworker/u16:3: 0% user + 1.7% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.6% 23173/kworker/u16:9: 0% user + 1.6% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 1.3% 23196/kworker/u16:14: 0% user + 1.3% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.6% 24530/kworker/u16:0: 0% user + 0.6% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.9% 10811/com.sec.phone: 0.5% user + 0.3% kernel / faults: 171 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.8% 20538/kworker/u16:4: 0% user + 0.8% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.8% 732/surfaceflinger: 0.5% user + 0.3% kernel / faults: 308 minor 16 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.6% 702/android.hardware.graphics.composer@2.1-service: 0.2% user + 0.3% kernel / faults: 96 minor 2 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.5% 695/android.hardware.bluetooth@1.0-service-qti: 0.1% user + 0.3% kernel / faults: 15 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.4% 401/cfinteractive: 0% user + 0.4% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.3% 20184/com.google.android.googlequicksearchbox:search: 0.3% user + 0% kernel / faults: 3639 minor 16 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.3% 3/ksoftirqd/0: 0% user + 0.3% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.3% 80/smem_native_rpm: 0% user + 0.3% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.2% 13990/logcat: 0% user + 0.1% kernel / faults: 29 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 1//init: 0.1% user + 0% kernel / faults: 221 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 23078/mdss_fb0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 29022/com.google.android.gms.persistent: 0.1% user + 0% kernel / faults: 543 minor 1 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 15/ksoftirqd/1: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 29/ksoftirqd/3: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 123/kswapd0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 710/android.hardware.wifi@1.0-service: 0% user + 0.1% kernel / faults: 14 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 273/kgsl_worker_thr: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 25576/kworker/0:1: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 7/rcu_preempt: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 3096/cds_mc_thread: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 89/kcompactd0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 26744/kworker/1:0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 3307/com.sec.android.inputmethod: 0% user + 0% kernel / faults: 134 minor 5 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 3927/iod: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 10/rcuop/0: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 95/system: 0% user + 0.1% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0.1% 624/servicemanager: 0% user + 0% kernel / faults: 4 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 26753/kworker/3:2: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 22/ksoftirqd/2: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 348/irq/305-fts_tou: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 25/rcuop/2: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 31420/adbd: 0% user + 0% kernel / faults: 642 minor 2 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 701/android.hardware.graphics.allocator@2.0-service: 0% user + 0% kernel / faults: 57 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 717/healthd: 0% user + 0% kernel / faults: 7 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 4187/com.sec.android.app.launcher: 0% user + 0% kernel / faults: 2727 minor 3 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 191/msm_serial_hs_0: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 2660/cnss-daemon: 0% user + 0% kernel / faults: 33 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 20087/com.google.android.gms: 0% user + 0% kernel / faults: 875 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 910/wlan_logging_th: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 27044/kworker/2:4: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 23224/com.android.vending: 0% user + 0% kernel / faults: 910 minor 3 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 18/rcuop/1: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 407/irq/181-spdm_bw: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 705/android.hardware.memtrack@1.0-service: 0% user + 0% kernel / faults: 12 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 5231/com.samsung.cmh:CMH: 0% user + 0% kernel / faults: 207 minor 3 major
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 26472/com.google.android.apps.docs: 0% user + 0% kernel / faults: 57 minor
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 79/dsps_smem_glink: 0% user + 0% kernel
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] E/ActivityManager( 2867): 0% 83/msm_wa
[mar. mars 30 10:06:25 CEST 2021][INFO] [SUB] W/ActivityManager( 2867): anr : com.hacare.ehacarebox,0
如您所见,列表ObservableList<BleDevice> ble_list_device = ble.startScanningDevices();
的大小为0。这可能会导致应用程序崩溃
这是主要的class源代码:
package com.hacare;
import com.gluonhq.attach.util.Constants;
import com.hacare.views.PrimaryView;
import com.hacare.views.SecondaryView;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.visual.Swatch;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends MobileApplication {
public static final String PRIMARY_VIEW = HOME_VIEW;
public static final String SECONDARY_VIEW = "test";
@Override
public void init() {
addViewFactory(PRIMARY_VIEW, () -> new PrimaryView().getView());
addViewFactory(SECONDARY_VIEW, () -> new SecondaryView().getView());
DrawerManager.buildDrawer(this);
}
@Override
public void postInit(Scene scene) {
Swatch.BLUE.assignTo(scene);
scene.getStylesheets().add(Main.class.getResource("style.css").toExternalForm());
((Stage) scene.getWindow()).getIcons().add(new Image(Main.class.getResourceAsStream("/icon.png")));
}
public static void main(String[] args) {
System.setProperty(Constants.ATTACH_DEBUG,"true");
launch(args);
}
}
理想情况下,连接后您应该等待连接状态,然后再开始询问配置文件列表。
这些是您可以使用的一些代码片段:
设备发现
BleService.create().ifPresent(ble -> {
ObservableList<BleDevice> devices = ble.startScanningDevices();
...
ble.stopScanningDevices();
});
设备连接
BleService.create().ifPresent(ble -> {
device.stateProperty().addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
if (State.STATE_CONNECTED.equals(device.getState())) {
// device connected, get profiles:
ObservableList<BleProfile> profiles = device.getProfiles();
...
device.stateProperty().removeListener(this);
}
}
});
ble.connect(device);
});
个人资料特征
ObservableList<BleCharacteristic> characteristics = profile.getCharacteristics();
...
// read characteristic
BleService.create().ifPresent(ble ->
ble.readCharacteristic(device, profile.getUuid(), characteristic.getUuid());
// subscribe characteristic
BleService.create().ifPresent(ble ->
ble.subscribeCharacteristic(device, profile.getUuid(), characteristic.getUuid());
// write characteristic
BleService.create().ifPresent(ble ->
ble.writeCharacteristic(device, profile.getUuid(), characteristic.getUuid(), bytes));
...
Android BLE Framework 中发生了一些变化,目前解决此问题的方法是首先将 pom.xml 文件中的附加版本更改为 4.0.12-SNAPSHOT。
<attach.version>4.0.12-SNAPSHOT</attach.version>
然后添加以下存储库:
<repository>
<id>Snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>