1.BluetoothController.java
public class BluetoothController {
private final BluetoothAdapter mAdapter;
public BluetoothController(){
mAdapter = BluetoothAdapter.getDefaultAdapter();
}
// 打开蓝牙设备
@SuppressLint("MissingPermission")
public void turnOnBlueTooth(Activity activity, int requestCode){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
activity.startActivityForResult(intent,requestCode);
}
// 查找未绑定的蓝牙设备
@SuppressLint("MissingPermission")
public void findDevice(){
assert (mAdapter!=null);
mAdapter.startDiscovery();
}
// 设置设备可见性
@SuppressLint("MissingPermission")
public void enableVisibily(Discoverydevice discoverydevice) {
// 设置设备名称
String deviceName = "MyBluetoothDevice"; // 替换为您希望显示的设备名称
mAdapter.setName(deviceName);
// 设置设备可见性持续时间为300秒
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
discoverydevice.startActivity(discoverableIntent);
}
}