@TargetApi(Build.VERSION_CODES.KITKAT) @RpcMinSdk(Build.VERSION_CODES.KITKAT) @Rpc( description = "Connects to a paired or discovered device with A2DP profile." + "If a device has been discovered but not paired, this will pair it." ) public void btA2dpConnect(String deviceAddress) throws Throwable { BluetoothDevice device = BluetoothAdapterSnippet.getKnownDeviceByAddress(deviceAddress); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST); mContext.registerReceiver(new PairingBroadcastReceiver(mContext), filter); Utils.invokeByReflection(sA2dpProfile, "connect", device); if (!Utils.waitUntil( () -> sA2dpProfile.getConnectionState(device) == BluetoothA2dp.STATE_CONNECTED, 120)) { throw new BluetoothA2dpSnippetException( "Failed to connect to device " + device.getName() + "|" + device.getAddress() + " with A2DP profile within 2min."); } }
/** * handle FM over BT connect state * * @param connectState * FM over BT connect state */ private void handleBtConnectState(int connectState) { if (!mIsPowerUp) { return; } switch (connectState) { case BluetoothA2dp.STATE_CONNECTED: //case BluetoothA2dp.STATE_PLAYING: //case BluetoothA2dp.STATE_CONNECTING: Log.d(TAG, "handleBtConnectState bt connected"); changeToEarphoneMode(); break; case BluetoothA2dp.STATE_DISCONNECTED: //case BluetoothA2dp.STATE_DISCONNECTING: Log.d(TAG, "handleBtConnectState bt disconnected"); changeToEarphoneMode(); break; default: Log.d(TAG, "invalid fm over bt connect state"); break; } }
private void registerFmBroadcastReceiver() { IntentFilter filter = new IntentFilter(); filter.addAction(SOUND_POWER_DOWN_MSG); filter.addAction(Intent.ACTION_SHUTDOWN); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_HEADSET_PLUG); /* Vanzo:tanglei on: Fri, 13 Mar 2015 21:22:01 +0800 */ //if (FeatureOption.VANZO_FEATURE_EARPHONE_KEY_ACTION) { // filter.addAction("com.mediatek.FMRadio.FMRadioService.NEXT_STATION"); //} // End of Vanzo:tanglei filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED); filter.addAction(ACTION_TOFMSERVICE_POWERDOWN); filter.addAction(ACTION_FROMATVSERVICE_POWERUP); mBroadcastReceiver = new FmServiceBroadcastReceiver(); Log.i(TAG, "Register broadcast receiver."); registerReceiver(mBroadcastReceiver, filter); }
@Override public void onServiceConnected(int profile, BluetoothProfile proxy) { Log.e("blueHeadsetListener", "onServiceConnected:" + profile); if (profile == BluetoothProfile.A2DP) { voiceMediator.setBluetoothA2dp((BluetoothA2dp) proxy); } else if (profile == BluetoothProfile.HEADSET) { voiceMediator.setBluetoothHeadset((BluetoothHeadset) proxy); } }
@Rpc(description = "Disconnects a device from A2DP profile.") public void btA2dpDisconnect(String deviceAddress) throws Throwable { BluetoothDevice device = getConnectedBluetoothDevice(deviceAddress); Utils.invokeByReflection(sA2dpProfile, "disconnect", device); if (!Utils.waitUntil( () -> sA2dpProfile.getConnectionState(device) == BluetoothA2dp.STATE_DISCONNECTED, 120)) { throw new BluetoothA2dpSnippetException( "Failed to disconnect device " + device.getName() + "|" + device.getAddress() + " from A2DP profile within 2min."); } }
@Override public void onServiceConnected(int profile, BluetoothProfile proxy) { try { if (profile == BluetoothProfile.HEADSET) { // bh = (BluetoothHeadset) proxy; // if (bh.getConnectionState(mTouchObject.bluetoothDevice) != BluetoothProfile.STATE_CONNECTED){ // bh.getClass() // .getMethod("connect", BluetoothDevice.class) // .invoke(bh, mTouchObject.bluetoothDevice); // } } 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(); } }
/** * Wrapper around some reflection code to get the hidden 'connect()' method * @return the connect(BluetoothDevice) method, or null if it could not be found */ private Method getConnectMethod () { try { return BluetoothA2dp.class.getDeclaredMethod("connect", BluetoothDevice.class); } catch (NoSuchMethodException ex) { Log.e(TAG, "Unable to find connect(BluetoothDevice) method in BluetoothA2dp proxy."); return null; } }
public void setBluetoothA2dp(BluetoothA2dp bluetoothA2dp) { this.bluetoothA2dp = bluetoothA2dp; }
@Override public BluetoothA2dp getBluetoothA2dp() { return bluetoothA2dp; }
public void onServiceConnected(int var1, BluetoothProfile profile) { sA2dpProfile = (BluetoothA2dp) profile; sIsA2dpProfileReady = true; }
@Override public void onServiceConnected(int i, BluetoothProfile bluetoothProfile) { bluetoothHandler .obtainMessage(BluetoothHandler.MESSAGE_A2DP_PROXY_RECEIVED,(BluetoothA2dp) bluetoothProfile) .sendToTarget(); }
@Override public void handleMessage(Message message) { switch (message.what) { case MESSAGE_READ: byte[] readBuf = (byte[]) message.obj; //get how many bytes were actually read. int datalength = message.arg1; String readMessage = new String(readBuf, 0, datalength); if(readBuf.length > 0) { if(mListener != null) mListener.onBluetoothDataReceived(readBuf, readMessage); } break; case MESSAGE_WAIT_FOR_CONNECTION: if(dialog != null) { dialog.setTitle(""); dialog.setMessage("Waiting..."); dialog.show(); } break; case MESSAGE_CONNECTION_MADE: if(dialog != null) { if(dialog.isShowing()) { dialog.dismiss(); if(shouldShowSnackbars && mActivity != null) { Snackbar.make(mActivity.findViewById(android.R.id.content), "Device connected.", Snackbar.LENGTH_SHORT).show(); } } } break; case MESSAGE_A2DP_PROXY_RECEIVED: BluetoothA2dp device = (BluetoothA2dp) message.obj; if(device != null && mListener != null) { mListener.onBluetoothA2DPRequested(device); } break; default: break; } }
@Override public void onServiceConnected(int i, BluetoothProfile bluetoothProfile) { if (mCallback != null) { mCallback.onA2DPProxyReceived((BluetoothA2dp) bluetoothProfile); } }
/** * 获取可操控蓝牙设备对象 **/ BluetoothA2dp getBluetoothA2dp();
/** * Called when a request is made to connect to an A2dp device. * @param bluetoothA2dp the a2dp device. */ public void onBluetoothA2DPRequested(BluetoothA2dp bluetoothA2dp) { }
public void onA2DPProxyReceived (BluetoothA2dp proxy);