1.需要声明权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
2.动态申请权限
private final String[] needPermissions = {
Manifest.permission.BLUETOOTH_ADVERTISE
};
if(ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, needPermissions, 1);
}
3.打开蓝牙,设置蓝牙300秒可见
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(intent);