监听打开的结果:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == ENABLE_BLUE) { if (resultCode == RESULT_OK) { Toast.makeText(this, "蓝牙开启成功", Toast.LENGTH_SHORT).show(); getBondedDevices(); } else if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "蓝牙开始失败", Toast.LENGTH_SHORT).show(); } } else { } } 1234567891011121314 1234567891011121314注册广播监听搜索的结果:
/**注册搜索蓝牙receiver*/ mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mFilter.addAction(BluetoothDevice.ACTION_FOUND); mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(mReceiver, mFilter); 12345 12345开始的搜索:
// 如果正在搜索,就先取消搜索 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } // 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回 mBluetoothAdapter.startDiscovery(); 123456 123456监听搜索的结果:
private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); /** 搜索到的蓝牙设备*/ if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 搜索到的不是已经配对的蓝牙设备 if (device.getBondState() != BluetoothDevice.BOND_BONDED) { BlueDevice blueDevice = new BlueDevice(); blueDevice.setName(device.getName()); blueDevice.setAddress(device.getAddress()); blueDevice.setDevice(device); setDevices.add(blueDevice); blueAdapter.setSetDevices(setDevices); blueAdapter.notifyDataSetChanged(); Log.d(MAINACTIVITY, "搜索结果......"+device.getName()); } /**当绑定的状态改变时*/ } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { /**搜索完成*/ } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { setProgressBarIndeterminateVisibility(false); Log.d(MAINACTIVITY, "搜索完成......"); hideProgressDailog(); } } }; 1234567891011121314151617181920212223242526272829303132333435 1234567891011121314151617181920212223242526272829303132333435配对工具类
public class BlueUtils { public BlueUtils(BlueDevice blueDevice) { this.blueDevice = blueDevice; } /** * 配对 */ public void doPair() { if(null == mOthHandler){ HandlerThread handlerThread = new HandlerThread("other_thread"); handlerThread.start(); mOthHandler = new Handler(handlerThread.getLooper()); } mOthHandler.post(new Runnable() { @Override public void run() { initSocket(); //取得socket try { socket.connect(); //请求配对 // mAdapterManager.updateDeviceAdapter(); } catch (IOException e) { e.printStackTrace(); } } }); } /** * 取消蓝牙配对 * @param device */ public static void unpairDevice(BluetoothDevice device) { try { Method m = device.getClass() .getMethod("removeBond", (Class[]) null); m.invoke(device, (Object[]) null); } catch (Exception e) { Log.d("BlueUtils", e.getMessage()); } } /** * 取得BluetoothSocket */ private void initSocket() { BluetoothSocket temp = null; try { Method m = blueDevice.getDevice().getClass().getMethod("createRfcommSocket", new Class[] {int.class}); temp = (BluetoothSocket) m.invoke(blueDevice.getDevice(), 1); //怪异错误: 直接赋值给socket,对socket操作可能出现异常, 要通过中间变量temp赋值给socket } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } socket = temp; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869注册监听配对结果的广播(使用同上面的注册代码)
/**注册搜索蓝牙receiver*/ mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); mFilter.addAction(BluetoothDevice.ACTION_FOUND); mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); registerReceiver(mReceiver, mFilter); 12345 12345开始配对
/** * 开始配对蓝牙设备 * * @param blueDevice */ private void startPariBlue(BlueDevice blueDevice) { BlueUtils blueUtils = new BlueUtils(blueDevice); blueUtils.doPair(); } 123456789 123456789监听配对结果:(使用同上面的广播接收者)
private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); /** 搜索到的蓝牙设备*/ if (action.equals(BluetoothDevice.ACTION_FOUND)) { ..... /**当绑定的状态改变时*/ } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); switch (device.getBondState()) { case BluetoothDevice.BOND_BONDING: Log.d(MAINACTIVITY, "正在配对......"); break; case BluetoothDevice.BOND_BONDED: Log.d(MAINACTIVITY, "完成配对"); hideProgressDailog(); /**开始连接*/ contectBuleDevices(); break; case BluetoothDevice.BOND_NONE: Log.d(MAINACTIVITY, "取消配对"); Toast.makeText(MainActivity.this,"成功取消配对",Toast.LENGTH_SHORT).show(); getBondedDevices(); break; default: break; } /**搜索完成*/ } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { .... } } }; 1234567891011121314151617181920212223242526272829303132333435363738 1234567891011121314151617181920212223242526272829303132333435363738连接设备
/** * 开始连接蓝牙设备 */ private void contectBuleDevices() { /**使用A2DP协议连接设备*/ mBluetoothAdapter.getProfileProxy(this, mProfileServiceListener, BluetoothProfile.A2DP); } 12345678 12345678监听连接的回调
/** * 连接蓝牙设备(通过监听蓝牙协议的服务,在连接服务的时候使用BluetoothA2dp协议) */ private BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() { @Override public void onServiceDisconnected(int profile) { } @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { try { if (profile == BluetoothProfile.HEADSET) { .... } else if (profile == BluetoothProfile.A2DP) { /**使用A2DP的协议连接蓝牙设备(使用了反射技术调用连接的方法)*/ a2dp = (BluetoothA2dp) proxy; if (a2dp.getConnectionState(currentBluetoothDevice) != BluetoothProfile.STATE_CONNECTED) { a2dp.getClass() .getMethod("connect", BluetoothDevice.class) .invoke(a2dp, currentBluetoothDevice); Toast.makeText(MainActivity.this,"请播放音乐",Toast.LENGTH_SHORT).show(); getBondedDevices(); } } } catch (Exception e) { e.printStackTrace(); } } }; 12345678910111213141516171819202122232425262728293031323334 12345678910111213141516171819202122232425262728293031323334Android 6.0的系统需要动态添加权限
/**判断手机系统的版本*/ if (Build.VERSION.SDK_INT >= 6.0) {//Build.VERSION.SDK_INT >= Build.VERSION_CODES.M if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ /**动态添加权限:ACCESS_FINE_LOCATION*/ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_CONSTANT); } } 12345678 12345678请求权限的回调
/**请求权限的回调:这里判断权限是否添加成功*/ /**请求权限的回调:这里判断权限是否添加成功*/ public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSION_REQUEST_CONSTANT: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.i("main","添加权限成功"); } return; } } }