蓝牙的开启:蓝牙的开启需要用到BluetoothAdapter蓝牙适配器,通过蓝牙适配器的返回值判断系统是否支持蓝牙的使用,然后通过BluetoothAdapter的isEnable()方法判断蓝牙的使用状态,开启蓝牙;
蓝牙的开启有两种方式,第一种是使用系统对话框,第二种是静默的开启方式。
第一种开启方式需要添加的蓝牙权限:
<!--调用系统对话框启动蓝牙需要添加的权限--> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
添加完权限后,在onCreate方法中实例化BluetoothAdapter蓝牙适配器,通过蓝牙适配器的返回值判断系统是否支持蓝牙的使用;接着在onStart方法中采用系统对话框的方式开启蓝牙,采用startActivityForResult方法,所以在onActivityResult方法中处理开启后的结果,这里采用吐司提醒处理,代码如下所示:
public class MainActivity extends AppCompatActivity { private static final String TAG = "bluetooth"; private BluetoothAdapter mBluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //判断设备是否支持蓝牙 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null){ Log.e(TAG, "Device not support bluetooth" ); }else{ Toast.makeText(this, "手机支持蓝牙设备!", Toast.LENGTH_SHORT).show(); } } //调用系统对话框开启蓝牙 @Override protected void onStart() { super.onStart(); if(!mBluetoothAdapter.isEnabled()){ //向系统请求开启蓝牙 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,10); }else{ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 10){ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启", Toast.LENGTH_SHORT).show(); } } }
第二种开启方式:第二种开启方式需要添加的权限有
<!--静默开启需要添加的权限--> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
因为第二种权限涉及危险权限,所以需要进行运行时处理,在进行完运行时处理完毕调用BluetoothAdapter.enable()方法开启蓝牙,代码如下所示:
public class MainActivity extends AppCompatActivity { private static final String TAG = "bluetooth"; private BluetoothAdapter mBluetoothAdapter; private static final int REQUEST_BLUETOOTH_PERMISSION = 10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestBluetoothPermission(); //判断设备是否支持蓝牙 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null){ Log.e(TAG, "Device not support bluetooth" ); }else{ Toast.makeText(this, "手机支持蓝牙设备!", Toast.LENGTH_SHORT).show(); } } @Override protected void onStart() { super.onStart(); mBluetoothAdapter.enable(); } /* //调用系统对话框开启蓝牙 @Override protected void onStart() { super.onStart(); if(!mBluetoothAdapter.isEnabled()){ //向系统请求开启蓝牙 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,10); }else{ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 10){ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启", Toast.LENGTH_SHORT).show(); } }*/ private void requestBluetoothPermission(){ //判断系统版本 if(Build.VERSION.SDK_INT >= 23){ //监测当前app是否拥有某个权限 int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION); //判断这个权限是否已经授权过 if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){ //判断是否需要,向用户解释,为什么要申请权限 if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)){ Toast.makeText(this, "Need bluttooth permission.", Toast.LENGTH_SHORT).show(); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},REQUEST_BLUETOOTH_PERMISSION); return; }else{ } }else{ } } } }
查找已配对设备:通过BluetoothAdapter的getBondedDevices()方法得到已配对的蓝牙设备,然后循环遍历log打印配对设备的设备名和mac地址
查找可匹配的设备:通过BluetoothAdapter的startDiscovery()方法来查找设备,查找的结果通过注册的广播接收器的action来接收处理;
基本使用的完整代码如下:
public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final int REQUEST_ENABLE_BT = 10; private BluetoothAdapter mBluetoothAdapter; private Button mScanButton; private Button mPairedDeviceButton; private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //通过action得到蓝牙扫描的结果 String action = intent.getAction(); Log.e(TAG, "action: "+action ); if(action.equals(BluetoothDevice.ACTION_FOUND)){ //log打印发现的新设备 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.e(TAG, "new decive name: "+device.getName() ); Log.e(TAG, "new device addr: "+device.getAddress() ); }else if(action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)){ //log打印扫描结束 Log.e(TAG, "discovery done"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mScanButton = (Button) findViewById(R.id.bt_scan); mScanButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //如果正在扫描,停止扫描重新开始扫描 if(mBluetoothAdapter.isDiscovering()){ mBluetoothAdapter.cancelDiscovery(); } mBluetoothAdapter.startDiscovery(); } }); mPairedDeviceButton = (Button) findViewById(R.id.bt_paired_device); mPairedDeviceButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); for (BluetoothDevice device:pairedDevices) { //log打印蓝牙设备的名称和地址 Log.e(TAG, "decive name: "+device.getName() ); Log.e(TAG, "device addr: "+device.getAddress() ); } } }); //判断设备是否支持蓝牙 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter == null){ Log.e(TAG, "Device not support bluetooth" ); }else{ Toast.makeText(this, "支持蓝牙设备!", Toast.LENGTH_SHORT).show(); } IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mBluetoothReceiver,filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mBluetoothReceiver); } //判断是否开启蓝牙 @Override protected void onStart() { super.onStart(); if(!mBluetoothAdapter.isEnabled()){ //向系统请求开启蓝牙 Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent,REQUEST_ENABLE_BT); }else{ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == REQUEST_ENABLE_BT){ //已经开启蓝牙 Toast.makeText(this, "蓝牙已经开启", Toast.LENGTH_SHORT).show(); } } }