扫描设备失败 android BLE
Scanning device fail android BLE
我正在尝试编写一个 android 应用程序,它是一个通过 BLE 聊天的应用程序。
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
我在 log cat 中收到了这条消息,但在扫描设备时没有触发 onLeScan()
方法。
D/BluetoothAdapter: startLeScan(): null
D/BluetoothAdapter:onClientRegistered() - status=0 clientIf=5
D/BluetoothAdapter: stopLeScan()
我的扫描设备代码是
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, device.getName(), Toast.LENGTH_SHORT)
.show();
}
});
}
};
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
当我检查我的设备规格时,它们只支持蓝牙智能,不支持外设模式。我的问题是是否可以在不支持外设模式的情况下相互连接支持 BLE 的移动设备 phone?还是我的代码有问题?
编辑
我有 3 台测试设备,华为 730 android 4.3,三星 galaxy core 2 android 4.4.2 和 Android 一台 android 6.0。 1.他们都遇到了我的问题。我还使用 BLEScanner 进行了测试,但没有检测到我所有的设备。此外,我无法通过 运行 BluetoothLeGatt 检测到我的所有设备,这是来自 android sdk.
的示例项目
我找到了答案。这是因为棒棒糖之前的设备不支持外设模式。作为外围设备的 phone 之一必须具有 android 5.0 (Lollipop) 及更高版本,以便它可以宣传其存在,创建 GATTServer 并让另一个 phone 将其连接为 central/client.
Make communication between 2 android phones over BLE
https://developer.android.com/about/versions/android-5.0.html#BluetoothBroadcasting
编辑
由于硬件要求,甚至一些棒棒糖及以上 phones 也不支持外设模式。
希望对你有用。
我正在尝试编写一个 android 应用程序,它是一个通过 BLE 聊天的应用程序。
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
我在 log cat 中收到了这条消息,但在扫描设备时没有触发 onLeScan()
方法。
D/BluetoothAdapter: startLeScan(): null
D/BluetoothAdapter:onClientRegistered() - status=0 clientIf=5
D/BluetoothAdapter: stopLeScan()
我的扫描设备代码是
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, device.getName(), Toast.LENGTH_SHORT)
.show();
}
});
}
};
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
当我检查我的设备规格时,它们只支持蓝牙智能,不支持外设模式。我的问题是是否可以在不支持外设模式的情况下相互连接支持 BLE 的移动设备 phone?还是我的代码有问题?
编辑
我有 3 台测试设备,华为 730 android 4.3,三星 galaxy core 2 android 4.4.2 和 Android 一台 android 6.0。 1.他们都遇到了我的问题。我还使用 BLEScanner 进行了测试,但没有检测到我所有的设备。此外,我无法通过 运行 BluetoothLeGatt 检测到我的所有设备,这是来自 android sdk.
的示例项目我找到了答案。这是因为棒棒糖之前的设备不支持外设模式。作为外围设备的 phone 之一必须具有 android 5.0 (Lollipop) 及更高版本,以便它可以宣传其存在,创建 GATTServer 并让另一个 phone 将其连接为 central/client.
Make communication between 2 android phones over BLE
https://developer.android.com/about/versions/android-5.0.html#BluetoothBroadcasting
编辑
由于硬件要求,甚至一些棒棒糖及以上 phones 也不支持外设模式。
希望对你有用。