gpt4 book ai didi

java - 用于发现蓝牙设备不工作的广播接收器

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:59 25 4
gpt4 key购买 nike

您好,我已按照 https://developer.android.com/guide/topics/connectivity/bluetooth 中的示例进行操作我的广播接收器可能无法正常工作。我在我的 Android 小米 A1 设备上运行 Android 8.1。

@Override
protected void onCreate(Bundle savedInstanceState) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Toast.makeText(MainActivity.this, deviceName, Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, deviceHardwareAddress, Toast.LENGTH_SHORT).show();
mDeviceList.add(deviceHardwareAddress);
ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,mDeviceList);
listV.setAdapter(arrayAdapter);
}
}
};

谢谢!

注意

对于 Android Api 21 广播接收器工作正常,我需要进行哪些更改才能使其适用于 Android 8.1?

最佳答案

在 list 中,我已添加权限和其他服务,例如启动停止蓝牙并查找配对设备工作正常...

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

关于java - 用于发现蓝牙设备不工作的广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52916048/

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