Android/Java
xml <!-- Android 12以下才需要定位权限, Android 9以下官方建议申请ACCESS_COARSE_LOCATION -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- Android 12在不申请定位权限时,必须加上android:usesPermissionFlags="neverForLocation",否则搜不到设备 -->
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Andoird蓝牙java:
cpp @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 请求权限
requestPermissions();
// settingBlueTooth();
// scanLeDevice(true);
// 初始化蓝牙适配器
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 开始搜索蓝牙设备
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return;
}
bluetoothAdapter.startDiscovery();
// 注册广播接收器来接收蓝牙搜索结果
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
bluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 从Intent中获取蓝牙设备对象
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 打印蓝牙设备的MAC地址
if (device != null) {
Log.i(TAG, "Found device: " + device.getAddress());
// 如果设备的MAC A4开头,就是我们的设备
if (device.getAddress().startsWith("A4")) {
Log.i(TAG, "Found our device: " + device.getAddress());
}
}
}
}
};
registerReceiver(bluetoothReceiver, filter);
} else {
Log.e(TAG, "Bluetooth is not available");
}
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止蓝牙设备搜索并注销广播接收器
if (bluetoothAdapter != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return;
}
bluetoothAdapter.cancelDiscovery();
}
unregisterReceiver(bluetoothReceiver);
}
请求权限requestPermissions
cpp private void requestPermissions() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
) {
// 如果没有获取到权限,请求权限
ActivityCompat.requestPermissions(this, new String[]{
android.Manifest.permission.BLUETOOTH,
android.Manifest.permission.BLUETOOTH_ADMIN,
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
}, PERMISSION_REQUEST_CODE);
}
} else {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
) {
// 如果没有获取到权限,请求权限
ActivityCompat.requestPermissions(this, new String[]{
android.Manifest.permission.BLUETOOTH_SCAN,
android.Manifest.permission.BLUETOOTH_ADVERTISE,
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
}, PERMISSION_REQUEST_CODE);
}
}
// 如果权限已经被授予,直接进行蓝牙功能检查和定位功能检查
}
@Override
protected void onDestroy() {
super.onDestroy();
// 停止蓝牙设备搜索并注销广播接收器
if (bluetoothAdapter != null) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return;
}
bluetoothAdapter.cancelDiscovery();
}
unregisterReceiver(bluetoothReceiver);
}
你如果需要帮助,请看这里:
csharphttps://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!