rk平臺(主)連接手機(從)連接不了,手機(主)連接rk平臺(從)可以成功,是什么原因?代碼如下
package com.cdn.bluetoothphone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import java.util.List;
import java.util.Set;
import android.bluetooth.BluetoothHeadsetClient;
import android.bluetooth.BluetoothHeadsetClientCall;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile.ServiceListener;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothA2dpSink;
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";//查詢當前連接的手機
private static final String FIND_PHONE_RESULT =
"com.viomi.fridge.curPhoneResult";//底層反饋實際連接的藍牙手機資訊
private static final String CONNECT_TO_PHONE =
"com.viomi.fridge.connectTo";//底層接收的切換藍牙電話狀態的廣播
private static final String ACTION_BOOT =
"android.intent.action.BOOT_COMPLETED";//開機廣播
private static final String ACTION1=
BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED; //藍牙連接成功標志
private static final String ACTION2=
BluetoothHeadsetClient.ACTION_CALL_CHANGED; //通話狀態發生變化
private static final String ACTION3=
BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED; //只要音頻狀態發生變化,就會發送意圖。
public static BluetoothDevice cur_device;
private static BluetoothAdapter mAdapter = null;
private static BluetoothHeadsetClient mclient; //藍牙電話類
private static BluetoothPbapClient mPbapClient; //藍牙通訊錄
List<BluetoothDevice> devices;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String actionStr=intent.getAction();
if (actionStr.equals(ACTION1)) {
Log.i(TAG, "-----ACTION1:--------"+actionStr);
int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
Intent PhoneIntent = new Intent(FIND_PHONE_RESULT);
if(state == 2){
Log.i(TAG, "device is connected...");
cur_device = (BluetoothDevice) intent.getExtra(BluetoothDevice.EXTRA_DEVICE);
PhoneIntent.putExtra("bleAddr",cur_device.getAddress());
context.sendBroadcast(PhoneIntent);
} else if(state == 0){
Log.i(TAG, "device is disconnected...");
cur_device=null;
PhoneIntent.putExtra("bleAddr","");
context.sendBroadcast(PhoneIntent);
}
} else if (actionStr.equals(ACTION2)) {
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)){ //打開藍牙開關
Log.i(TAG, "---------------FIND_PHONE-------"+actionStr);
Intent PhoneIntent = new Intent(FIND_PHONE_RESULT);
if(cur_device != null){
PhoneIntent.putExtra("bleAddr",cur_device.getAddress());
context.sendBroadcast(PhoneIntent);
} else {
PhoneIntent.putExtra("bleAddr","");
context.sendBroadcast(PhoneIntent);
}
} else if(actionStr.equals(CONNECT_TO_PHONE)){ //配對成功
Log.i(TAG, "--------------------CONNECT_TO_PHONE-----------"+actionStr);
String bleAddr=intent.getStringExtra("bleAddr"); //mac地址
String toConnect=intent.getStringExtra("status"); //連接狀態1連接,0斷開
Log.i(TAG,"click device:mac-->"+bleAddr+",status-->"+toConnect);
if(mAdapter==null){
mAdapter=BluetoothAdapter.getDefaultAdapter(); //獲取默認BluetoothAdapter
mAdapter.enable(); //強行打開
Log.i(TAG,"bluetooth enable >>>>>>>>>"+mAdapter.isEnabled());
mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.HEADSET_CLIENT);
mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.PBAP_CLIENT);
}
Set<BluetoothDevice> bleDevices = mAdapter.getBondedDevices(); //所有配對的設備
Log.i(TAG,"connect ========================================================");
for(BluetoothDevice device : bleDevices){
Log.i(TAG,"paried device name>>>>>>>>>"+device.getName()+",address>>>>>>>>>>>"+device.getAddress());
if(device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.PHONE
&&device.getAddress().equals(bleAddr)){
Log.i(TAG,"start connect");
if(toConnect.equals("1")){
Log.i(TAG, device.getName()+" is 1,bluetooth start connect--------------------");
if(mclient!=null)
{
Log.i(TAG,"mclient start connect:--------------------"+mclient.connect(device));
}
if(mPbapClient!=null){
Log.i(TAG,"mPbapClient start connect:--------------------"+mPbapClient.connect(device));
}
}else { //status=0,斷開
Log.i(TAG,"phone status is 0,bluetooth start disconnect--------------------");
if(mclient!=null){
Log.i(TAG,"mclient start disconnect:-------------------"+mclient.disconnect(device));
}
if(mPbapClient!=null){
Log.i(TAG,"mPbapClient start disconnect:----------"+mPbapClient.disconnect(device));
}
}
}
}
Log.i(TAG,"connect end========================================================");
} else if(actionStr.equals(ACTION_BOOT)){
Log.i(TAG, "boot>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+actionStr);
mAdapter = BluetoothAdapter.getDefaultAdapter();
mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.HEADSET_CLIENT);
mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.PBAP_CLIENT);
}
}
BluetoothProfile.ServiceListener mServiceListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
// TODO Auto-generated method stub
if (profile == BluetoothProfile.HEADSET_CLIENT) {
mclient=(BluetoothHeadsetClient)proxy;
}else if(profile == BluetoothProfile.PBAP_CLIENT){
mPbapClient=(BluetoothPbapClient) proxy;
}
}
@Override
public void onServiceDisconnected(int profile) {
// TODO Auto-generated method stub
if (profile == BluetoothProfile.HEADSET_CLIENT) {
mclient = null;
}
else if(profile == BluetoothProfile.PBAP_CLIENT){
mPbapClient= null;
}
}
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/89962.html
標籤:Android
