Home > Mobile >  Android8.1 bluetooth connection problem
Android8.1 bluetooth connection problem

Time:09-28

Fairly rk platform (main) link mobile phones (from), mobile phone (main) connection fairly rk platform (from) can be successful, what reason be? The code below
 package com. The CDN. Bluetoothphone; 

Import android. Content. BroadcastReceiver;
The import android. The content. The Context;
The import android. Content. Intent;
The import android. Sax. StartElementListener;
The import android. Util. Log;

import java.util.List;
import java.util.Set;

The import android. Bluetooth. BluetoothHeadsetClient;
The import android. Bluetooth. BluetoothHeadsetClientCall;
The import android. Bluetooth. BluetoothProfile;
The import android. Bluetooth. BluetoothAdapter;
The import android. Bluetooth. BluetoothDevice;
The import android. Bluetooth. BluetoothProfile. ServiceListener;
The import android. Bluetooth. BluetoothClass;
The import android. Bluetooth. BluetoothHeadset;
The import android. Bluetooth. BluetoothA2dp;
The import android. Bluetooth. BluetoothA2dpSink;
The import android. Bluetooth. BluetoothPbapClient;

Public class CallBroadcastReceiver extends BroadcastReceiver {

Public static final String TAG=
"Wangp";
Private static final String FIND_PHONE=
"Com. Viomi. Fridge. CurPhone";//query the current connection phone
Private static final String FIND_PHONE_RESULT=
"Com. Viomi. Fridge. CurPhoneResult";//underlying feedback actual bluetooth phone connection information
Private static final String CONNECT_TO_PHONE=
"Com. Viomi. Fridge. ConnectTo";//the underlying access switch bluetooth phone state radio
Private static final String ACTION_BOOT=
"Android. Intent. Action. BOOT_COMPLETED";//boot broadcast
Private static final String ACTION1=
BluetoothHeadsetClient. ACTION_CONNECTION_STATE_CHANGED;//bluetooth connection successfully sign
Private static final String ACTION2=
BluetoothHeadsetClient. ACTION_CALL_CHANGED;//call state change
Private static final String ACTION3=
BluetoothHeadsetClient. ACTION_AUDIO_STATE_CHANGED;//as long as the state of the audio changes, it will send intent,
Public static BluetoothDevice cur_device;
Private static BluetoothAdapter mAdapter=null;
Private static BluetoothHeadsetClient McLient;//bluetooth phone
Private static BluetoothPbapClient mPbapClient;//bluetooth address book

List Devices;

@ Override
Public void onReceive (Context Context, Intent Intent) {
//TODO Auto - generated method stub
String actionStr=intent. GetAction ();
{if (actionStr. Equals (ACTION1))
The Log i. (TAG, "-- -- -- -- -- ACTION1: -- -- -- -- -- -- -- --" + actionStr);
Int state=intent. GetIntExtra (BluetoothProfile EXTRA_STATE, 1);
Intent PhoneIntent=new Intent (FIND_PHONE_RESULT);
If (state==2) {
The Log. I (TAG, "device is connected... ");
Cur_device=(BluetoothDevice) intent. GetExtra (BluetoothDevice. EXTRA_DEVICE);
PhoneIntent. PutExtra (" bleAddr ", cur_device getAddress ());
The context. SendBroadcast (PhoneIntent);
{} else if (state==0)
The Log. I (TAG, "device is disconnected... ");
Cur_device=null;
BleAddr PhoneIntent. PutExtra (" ", "");
The context. SendBroadcast (PhoneIntent);
}
} else if (actionStr equals (ACTION2)) {
The Log i. (TAG, "-- -- -- -- -- ACTION2: bluetooth phone... -- -- -- -- -- -- -- -- -- -- -- -- -- "+ actionStr);
} else if (actionStr equals (ACTION3)) {
Log. I (TAG, "-- -- -- -- -- ACTION3 -- -- -- -- -- -- -- -- -- --" + actionStr);
Int state=intent. GetIntExtra (BluetoothProfile EXTRA_STATE, 1);
} else if (actionStr equals (FIND_PHONE)) {//open the bluetooth switch
The Log i. (TAG, "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- FIND_PHONE -- -- -- -- -- -- --" + actionStr);
Intent PhoneIntent=new Intent (FIND_PHONE_RESULT);
If (cur_device!=null) {
PhoneIntent. PutExtra (" bleAddr ", cur_device getAddress ());
The context. SendBroadcast (PhoneIntent);
} else {
BleAddr PhoneIntent. PutExtra (" ", "");
The context. SendBroadcast (PhoneIntent);
}
} else if (actionStr equals (CONNECT_TO_PHONE)) {//matching success
Log. I (TAG, "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- CONNECT_TO_PHONE -- -- -- -- -- -- -- -- -- -- -" + actionStr);
String bleAddr=intent. GetStringExtra (" bleAddr ");//MAC address
String toConnect=intent. GetStringExtra (" status ");//link state 1, 0 disconnect
Log. I (TAG, "click device: MAC - & gt;" + bleAddr + ", the status - & gt;" + toConnect);
If (mAdapter==null) {
MAdapter=BluetoothAdapter. GetDefaultAdapter ();//get the default BluetoothAdapter
MAdapter. The enable ();//forced open
The Log. I (TAG, "bluetooth enable & gt;>>>> & gt;" + mAdapter. IsEnabled ());
MAdapter. GetProfileProxy (context. GetApplicationContext (), mServiceListener, BluetoothProfile. HEADSET_CLIENT);
MAdapter. GetProfileProxy (context. GetApplicationContext (), mServiceListener, BluetoothProfile. PBAP_CLIENT);
}
Set BleDevices=mAdapter. GetBondedDevices ();//all matching equipment
Log. I (TAG, "connect========================================================");
For (BluetoothDevice device: bleDevices) {
The Log. I (TAG, "paried device name>>>> & gt;" + device. The getName () + ", address>>>>> & gt;" + device. GetAddress ());
If (device. GetBluetoothClass (.) getMajorDeviceClass ()==BluetoothClass. The device. The Major. The PHONE
& & Device. GetAddress () equals (bleAddr)) {
The Log i. (TAG, "start the connect");
If (toConnect. Equals (" 1 ")) {
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related