获取地理位置权限
private void checkVersion() {
if (Build.VERSION.SDK_INT >= 23) {
int checked = checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
if (checked != PackageManager.PERMISSION_GRANTED) {
Log.e("TAG", "---------------没有权限");
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PER);
} else {
Log.e("TAG", "---------------有权限");
openBluetooth();
}
} else {
openBluetooth();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, 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", "-------------蓝牙关闭状态,正在打开");
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);
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", "--------------可被发现被拒绝");
}
}
扫描设备
private void scanBluetooth() {
if (bluetoothAdapter.isDiscovering()) {
Log.e("TAG", "----正在扫描中");
}else if (bluetoothAdapter.isEnabled()){
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
}
}
}