gpt4 book ai didi

java - 发现蓝牙设备

转载 作者:行者123 更新时间:2023-11-30 12:04:13 25 4
gpt4 key购买 nike

此代码用于搜索和发现蓝牙设备
我在 BroadcastReceiver 上做 Toast 他们甚至不显示它(android 7 猜测我只是把它放在 mainfist 上的许可问题)

    Button button;
ListView listView;
BluetoothAdapter mBluetoothAdapter;
Integer requestbluetooth=1;
ArrayList<String> arrayList;
ArrayAdapter<String> Adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

arrayList =new ArrayList<>();
button=findViewById(R.id.printButton);
listView=findViewById(R.id.listView);

Adapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(Adapter);

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);


button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mBluetoothAdapter.startDiscovery();
}
});
}

BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress();
arrayList.add(deviceName);
Adapter.notifyDataSetChanged();
}
}
};

最佳答案

自 Android 6.0(API 级别 23)起,您必须在运行时请求权限。大多数应用程序会在用户首次启动应用程序时执行此操作。

要检查您是否已经拥有权限,请使用

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_SMS);

要请求许可,请使用

ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_SMS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);

关于java - 发现蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233846/

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