获取地理位置权限
private void checkVersion() {
//获取蓝牙
//必须要权限, 并且是危险权限 (地理位置 6.0版本之后,必须地理位置的权限)。
//必须要判断用户是否给了权限
//6.0以后,才需要检测权限。
if (Build.VERSION.SDK_INT >= 23) {
//只有超过23的手机才会执行该代码块
int checked = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
//判断是否是同意还是拒绝
if (checked != PackageManager.PERMISSION_GRANTED) {
Log.e("TAG", "---------------没有权限");
//用户在第一次安装的时候,没有给定权限。
//动态想用户获取权限
//String[]数组就是需要的权限,int 和我们的startActivityForResult相同
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PER);
} else {
Log.e("TAG", "---------------有权限");
//如果用户在初次安装的时候,同意了权限,也可以直接获取
openBluetooth();
}
} else {
//如果低于23,直接获取蓝牙设备
openBluetooth();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//String[] permissions 对应请求的权限
//int[] grantResults 对应上面的返回结果
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//如果点击同意,执行这里
Log.e("TAG", "-----------用户同意的请求");
openBluetooth();
} else {
//拒绝
Log.e("TAG", "-----------用户拒绝请求");
Toast.makeText(this, "地理位置被拒绝,蓝牙不能使用", Toast.LENGTH_SHORT).show();
finish();
}
直接打开蓝牙
if (bluetoothAdapter.isEnabled()) {
Log.e("TAG", "-------------蓝牙已经打开,不用再次打开");
} else {
Log.e("TAG", "-------------蓝牙关闭状态,正在打开");
//6.0第一次打开的需要权限
bluetoothAdapter.enable();
}
建议使用意图打开蓝牙
//本机的蓝牙设备
BluetoothAdapter bluetoothAdapter;
//其他设备
List<BluetoothDevice> deviceList = new ArrayList<>();
// 打开蓝牙设备
private void openBluetooth() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_OPEN_BLUETOOTH);
} else {
Log.e("TAG", "-----------蓝牙已经被打开,不需要重复打开");
}
}
响应用户的选择
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PERMISSION_BLUETOOTH && resultCode == RESULT_OK){
//用户同意打开蓝牙,之后扫描蓝牙设备
scanBluetooth();
}else{
Toast.makeText(mainActivity, R.string.openBlue, Toast.LENGTH_SHORT).show();
Log.e("TAG", "---------用户拒绝了打开蓝牙");
}
}
可被发现
//让其他设备可以找到我
//如果这一步蓝牙没有打开,没关系,会自动打开
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//默认120秒可被发现
//修改可被发现的时间
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 600);
startActivityForResult(intent, REQUEST_DISCOVERABLE);
if (requestCode == REQUEST_DISCOVERABLE) {
Log.e("TAG", "--------------" + resultCode);
if (resultCode > 0) {
//可被发现被打开
Log.e("TAG", "-------------可被发现被打开");
} else {
Log.e("TAG", "--------------可被发现被拒绝");
}
}
扫描设备
//在4.4以后,就使用广播接收者来接受设备,而不是使用回调监听
private void scanBluetooth() {
if (bluetoothAdapter.isDiscovering()) {
Log.e("TAG", "----正在扫描中");
}else if (bluetoothAdapter.isEnabled()){
//1,蓝牙必须开启 2,该方法是异步方法 3,每搜到一部设备,发一次广播
bluetoothAdapter.startDiscovery();
}else{
Toast.makeText(mainActivity, R.string.openBlue2, Toast.LENGTH_SHORT).show();
}
}
onStart中注册广播,先于onCreate执行
@Override
protected void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
//开始扫描
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
//结束扫描
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//找到蓝牙的广播
filter.addAction(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver,filter);
}
onStop中解除注册,后于onCreate执行
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(receiver);
}
成员变量,先于所有方法执行
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()){
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
Log.e("TAG", "-----------蓝牙开始扫描");
menuFragment.startRomateAnim();
menuFragment.clearBluetoothDevice();
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Log.e("TAG", "-----------蓝牙扫描结束");
menuFragment.stopRomateAnim();
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
break;
case BluetoothDevice.ACTION_FOUND:
//数据在intent中携带,已经实现了parcelable
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
menuFragment.addBluetoothDevice(device);
//String name = device.getName() == null ? "匿名" : device.getName();
//Log.e("TAG", "----------找到一台蓝牙设备:" + name + device.getAddress());
//deviceList.add(device);
//adapter.notifyDataSetChanged();
break;
}
}
};