蓝牙连接问题

yhm2046 中级 2019-11-01 08:15:51
android8.1 rk3326平台

二次开发界面需要连接蓝牙,现在问题是第一次需要在手机上面连接蓝牙才能成功连接,如果在rk平台上面首次连接会不成功,但是在setting原生界面的蓝牙设置里面是成功的,请问如何解决?
logcat:
--------- beginning of main
11-01 20:07:56.875 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger expire 14 lines
11-01 20:07:56.875 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:56.875 386 607 I WifiService: getWifiEnabledState uid=10025
11-01 20:07:56.890 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.207 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 36 lines
11-01 20:07:57.207 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.211 1349 2598 I FloatViewService: getRunningPackageNamecom.viomi.fridge.vertical
11-01 20:07:57.224 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.240 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 2 lines
11-01 20:07:57.240 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.247 1056 1304 E viomi_serial_jni: return read length =0 hex data=
11-01 20:07:57.256 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.256 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.270 386 607 D WificondControl: Scan result ready event
11-01 20:07:57.273 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.290 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 2 lines
11-01 20:07:57.291 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.314 386 537 W WifiConfigManager: Looking up network with invalid networkId -1
11-01 20:07:57.323 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.709 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 42 lines
11-01 20:07:57.710 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.728 1056 1327 I accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: try send:DATA dataId:14 appkey:umeng:5a7cf79cf43e481b2a00045c
11-01 20:07:57.729 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.729 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.728 1056 1327 D awcn.SessionCenter: |[seq:umeng:5a7cf79cf43e481b2a00045c] getInternal u:https://umengacs.m.taobao.com TypeClass:SPDY timeout:60000
11-01 20:07:57.737 1056 1327 D awcn.StrategyCenter: getSchemeByHost host:umengacs.m.taobao.com scheme:https
11-01 20:07:57.738 1056 1327 D awcn.SessionRequest: |[seq:umeng:5a7cf79cf43e481b2a00045c.AWCN30] SessionRequest start host:https://umengacs.m.taobao.com type:SPDY
11-01 20:07:57.746 1056 1304 E viomi_serial_jni: return read length =0 hex data=
11-01 20:07:57.749 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.749 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.765 1056 1327 D awcn.SessionRequest: |[seq:umeng:5a7cf79cf43e481b2a00045c.AWCN30] network is not available, can't create session NetworkStatusHelper.isConnected():false
11-01 20:07:57.768 1056 1327 E awcn.SessionCenter: |[seq:umeng:5a7cf79cf43e481b2a00045c] [Get]no network url:https://umengacs.m.taobao.com
11-01 20:07:57.768 1056 1327 E accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: reSend dataid:14 retryTimes:4
11-01 20:07:57.771 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.771 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:57.774 1056 1327 D accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: send message, DATA delay:2000 dataId:14
11-01 20:07:57.775 1056 1327 E accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: cancel customDataId:ctrl_bindapp
11-01 20:07:57.775 1056 1327 D accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: sendSucc false dataId:14
11-01 20:07:57.788 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.234 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 50 lines
11-01 20:07:58.234 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.248 1056 1304 E viomi_serial_jni: return read length =0 hex data=
11-01 20:07:58.251 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.251 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.259 1349 2598 I FloatViewService: getRunningPackageNamecom.viomi.fridge.vertical
11-01 20:07:58.270 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.490 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 26 lines
11-01 20:07:58.491 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.493 2696 2745 W bt_btm_ble: btm_ble_process_adv_pkt_cont device no longer discoverable, discarding advertising packet
11-01 20:07:58.507 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.742 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 28 lines
11-01 20:07:58.742 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:58.749 1056 1304 E viomi_serial_jni: return read length =0 hex data=
11-01 20:07:58.758 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.240 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 54 lines
11-01 20:07:59.240 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.242 1056 1303 E viomi_serial_jni: write length =8 hex data=f5 38 20 00 00 00 00 4d
11-01 20:07:59.245 1056 1304 E viomi_serial_jni: return read length =0 hex data=
11-01 20:07:59.258 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.290 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 4 lines
11-01 20:07:59.290 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.302 1349 2598 I FloatViewService: getRunningPackageNamecom.viomi.fridge.vertical
11-01 20:07:59.306 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.740 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 52 lines
11-01 20:07:59.740 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.746 1056 1304 E viomi_serial_jni: read this time length =8 tmp=0
11-01 20:07:59.746 1056 1304 E viomi_serial_jni: return read length =8 hex data=f5 38 20 00 00 00 00 4d
11-01 20:07:59.757 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.758 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.776 1056 1327 I accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: try send:DATA dataId:14 appkey:umeng:5a7cf79cf43e481b2a00045c
11-01 20:07:59.777 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.777 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.786 1056 1327 D awcn.SessionCenter: |[seq:umeng:5a7cf79cf43e481b2a00045c] getInternal u:https://umengacs.m.taobao.com TypeClass:SPDY timeout:60000
11-01 20:07:59.786 1056 1327 D awcn.StrategyCenter: getSchemeByHost host:umengacs.m.taobao.com scheme:https
11-01 20:07:59.787 1056 1327 D awcn.SessionRequest: |[seq:umeng:5a7cf79cf43e481b2a00045c.AWCN31] SessionRequest start host:https://umengacs.m.taobao.com type:SPDY
11-01 20:07:59.790 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.790 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:07:59.792 1056 1327 D awcn.SessionRequest: |[seq:umeng:5a7cf79cf43e481b2a00045c.AWCN31] network is not available, can't create session NetworkStatusHelper.isConnected():false
11-01 20:07:59.792 1056 1327 E awcn.SessionCenter: |[seq:umeng:5a7cf79cf43e481b2a00045c] [Get]no network url:https://umengacs.m.taobao.com
11-01 20:07:59.792 1056 1327 D accs.MsgRecv_umeng:5a7cf79cf43e481b2a00045c: onResult command:1 erorcode:-11
11-01 20:07:59.795 1056 1327 D accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: send message, DATA delay:2000 dataId:14
11-01 20:07:59.805 1056 1327 D accs.accs.BindAppStatistic: eventId=66001;arg1=XbwfjNmuEsoDANYlGc7csFr9;arg2=null;arg3=221;args={ret=n, device_id=XbwfjNmuEsoDANYlGc7csFr9, fail_reasons=-11, bind_date=1572610079796, push_token=}
11-01 20:07:59.807 1056 1327 D accs.UTMini: commitEvent fail java.lang.NoClassDefFoundError: Failed resolution of: Lcom/ut/mini/internal/UTOriginalCustomHitBuilder;
11-01 20:07:59.807 1056 1327 D accs.InAppConn_umeng:5a7cf79cf43e481b2a00045c: sendSucc false dataId:14
11-01 20:07:59.824 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:08:00.247 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 42 lines
11-01 20:08:00.248 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:08:00.248 1056 1304 E viomi_serial_jni: return read length =0 hex data=
11-01 20:08:00.262 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:08:00.307 234 234 I chatty : uid=1000(system) /system/bin/surfaceflinger identical 4 lines
11-01 20:08:00.307 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:08:00.330 1349 2598 I FloatViewService: getRunningPackageNamecom.viomi.fridge.vertical
11-01 20:08:00.342 234 234 E HWComposer: getPresentFence failed for invalid display -1
11-01 20:08:00.417 234 234 I chatty : uid=1000(system) /system/bin
...全文
3788 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhm2046 中级 2019-11-01
  • 打赏
  • 举报
回复
代码:
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 = "sendCallChangedIntent: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";//与手机连接或断开连接
	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
		if (intent.getAction().equals(BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED)) {// 连接远程设备广播
			// 0:disconneted 1:connecting 2:connected 3:disconnecting
			int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
			Log.i(TAG,"BluetoothHeadsetClient.ACTION_CONNECTION_STATE_CHANGED");
			Intent PhoneIntent = new Intent(FIND_PHONE_RESULT);
			if(state == 2){
				Log.i(TAG,"=============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,"=============disconnected===============");
				cur_device=null;
				PhoneIntent.putExtra("bleAddr","");
				context.sendBroadcast(PhoneIntent);
			}
		} else if (intent.getAction().equals(BluetoothHeadsetClient.ACTION_CALL_CHANGED)) {
			Log.i(TAG,"??????????==BluetoothHeadsetClient.ACTION_CALL_CHANGED");
			Object state = intent.getExtra(BluetoothHeadsetClient.EXTRA_CALL);// 注:getExtra()返回Object类型
			BluetoothHeadsetClientCall ss = (BluetoothHeadsetClientCall) state;// 负责蓝牙通话的状态,比如是来电还是去电等
			Log.e(TAG, " Object = " + String.valueOf(state));
			Log.e(TAG, "ss.mId = " + String.valueOf(ss.getId()));
			Log.e(TAG, "ss.mState = " + String.valueOf(ss.getState()));// 获取来电/去电/接通/挂断等状态
			Log.e(TAG, "ss.mNumber = " + String.valueOf(ss.getNumber()));// 获取来电/去电的电话号码
			Log.e(TAG, " ss.mOutgoing = " + String.valueOf(ss.isOutgoing()));
			
			if((ss.getState() == 4) || (ss.getState() == 7) || (ss.getState() == 0)){
				
				if((ss.getState() == 0) && !(MainActivity.activityIsactive)){
					Log.e(TAG,"============2==============");
					Log.i(TAG,"==============wangp connecting===============");
				    //mclient.disconnectAudio();
					return;
				}
				if((ss.getState() == 7) && !(MainActivity.activityIsactive)) {
					Log.e(TAG,"============3==============");
					Log.i(TAG,"==============wangp disconnecting===============");
					return;
				}
				Intent mintent = new Intent(context, MainActivity.class);
				mintent.putExtra("number", String.valueOf(ss.getNumber()));
				mintent.putExtra("state", ss.getState());
				context.startActivity(mintent);
			}
			else if(MainActivity.isMeReCallFlag && ( (ss.getState() == 0))){
				//Log.e(TAG,"============11111==============");
				Log.i(TAG,"==============wangp 90===============");
				Intent mintent = new Intent(context, MainActivity.class);
				mintent.putExtra("number", String.valueOf(ss.getNumber()));
				mintent.putExtra("state", ss.getState());
				context.startActivity(mintent);
			}

		} else if (intent.getAction().equals(BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED)) {
			Log.i(TAG,"BluetoothHeadsetClient.ACTION_AUDIO_STATE_CHANGED");
			int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
			Log.i(TAG,"ACTION_AUDIO_STATE_CHANGED: state = "+ String.valueOf(state));
		} else if(intent.getAction().equals(FIND_PHONE)){	
			Log.i(TAG,"FIND_PHONE");
			Intent PhoneIntent = new Intent(FIND_PHONE_RESULT);
			if(cur_device != null){
				PhoneIntent.putExtra("bleAddr",cur_device.getAddress());
				Log.i(TAG,"==============cur_device != null==============="+cur_device.getAddress());
                context.sendBroadcast(PhoneIntent);
			} else {
				Log.i(TAG,"==============cur_device == null================");
				PhoneIntent.putExtra("bleAddr","");
				context.sendBroadcast(PhoneIntent);
			}
		} else if(intent.getAction().equals(CONNECT_TO_PHONE)){	
			Log.i(TAG,"CONNECT_TO_PHONE");
			final String bleAddr=intent.getStringExtra("bleAddr");
            final String toconnect= intent.getStringExtra("status");
            Log.i(TAG,"CONNECT_TO_PHONE==========wangp bleAddr:"+bleAddr+",toconnect="+toconnect);	
			if(mAdapter == null){
				Log.i(TAG,"========mAdapter == null:bleAddr:"+bleAddr+",status:"+toconnect);
				mAdapter=BluetoothAdapter.getDefaultAdapter();
			    mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.HEADSET_CLIENT);	//耳机客户端
				mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.PBAP_CLIENT);	//电话薄对象
			}
			//返回绑定(配对)到本地适配器的BluetoothDevice对象集。
            Set<BluetoothDevice> bleDevices = mAdapter.getBondedDevices();
            for (BluetoothDevice device : bleDevices) {
            	Log.i(TAG,"=============BluetoothDevice list:===============");
                if(device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.PHONE
				&&device.getAddress().equals(bleAddr)){
					Log.i(TAG,"client p="+mclient);
					Log.i(TAG,"mPbapClient=="+mPbapClient);
					if(toconnect.equals("1")){	//连接成功
						Log.i(TAG,"============connected success==============");
						if(mclient != null)
							Log.i(TAG,"=====BluetoothHeadsetClient!=null==============");
							mclient.connect(device);
						if(mPbapClient !=null){
							Log.i(TAG,"======BluetoothPbapClient!=null==============");
						    mPbapClient.connect(device);
						}
					}
					else {	
						Log.i(TAG,"=========connected fail!==============");
						if(mclient != null)
							Log.i(TAG,"=========BluetoothHeadsetClient!=null===============");
							mclient.disconnect(device);
						if(mPbapClient !=null)
							Log.i(TAG,"=========BluetoothPbapClient!=null==============");
						    mPbapClient.disconnect(device);
					}
	
				
				}
			}
		} else if(intent.getAction().equals(ACTION_BOOT)){
           	    mAdapter = BluetoothAdapter.getDefaultAdapter();	//默认蓝牙适配器
				//Headset Client - HFP HF Role 耳机客户端,PBAP_CLIENT - 电话薄访问
                mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.HEADSET_CLIENT); //实现接口类型参数
				mAdapter.getProfileProxy(context.getApplicationContext(),mServiceListener, BluetoothProfile.PBAP_CLIENT);
				Log.i(TAG,"ACTION_BOOT");
		}
	}

//蓝牙监听器具体接口类型实现
	BluetoothProfile.ServiceListener mServiceListener = new BluetoothProfile.ServiceListener() {

	/**
		代理对象成功连接时候回调
	profile	int: - One of BluetoothProfile.HEADSET or BluetoothProfile.A2DP
	proxy	BluetoothProfile: - One of BluetoothHeadset or BluetoothA2dp
	**/
		@Override
		public void onServiceConnected(int profile, BluetoothProfile proxy) {	
			// TODO Auto-generated method stub

			if (profile == BluetoothProfile.HEADSET_CLIENT) {	//耳机客户端
				Log.i(TAG,"BluetoothProfile.HEADSET_CLIENT=========BluetoothProfile.HEADSET_CLIENT:"+profile);
				mclient=(BluetoothHeadsetClient)proxy;
				Log.i(TAG,"client p="+mclient);
				devices = mclient.getConnectedDevices();
				for (BluetoothDevice device : devices) {
					Log.i(CallBroadcastReceiver.TAG,"-------onServiceConnected------------"+device.getName());
				}
				
			}else if(profile == BluetoothProfile.PBAP_CLIENT){
				Log.i(TAG,"==============BluetoothProfile.PBAP_CLIENT:"+profile);
				mPbapClient=(BluetoothPbapClient) proxy;
				Log.i(TAG,"mPbapClient=="+mPbapClient);
			}else if(profile ==  BluetoothProfile.HEADSET){
				Log.i(TAG,"==============BluetoothProfile.HEADSET:"+profile);
			}else if(profile ==  BluetoothProfile.A2DP){
				Log.i(TAG,"==============BluetoothProfile.A2DP:"+profile);
			}
		}
		/**
		蓝牙断开回调.	profile:
		**/
		@Override
		public void onServiceDisconnected(int profile) {
			// TODO Auto-generated method stub
			Log.i(TAG,"-------onServiceDisconnected:------------"+profile);
			if (profile == BluetoothProfile.HEADSET_CLIENT) {
				Log.i(TAG,"==============wangp 201===============");
				mclient = null;
			}else if(profile == BluetoothProfile.PBAP_CLIENT){
				Log.i(TAG,"==============wangp 204===============");
				mPbapClient= null;
			}else if(profile ==  BluetoothProfile.HEADSET){
				Log.i(TAG,"==============BluetoothProfile.HEADSET:"+profile);
			}else if(profile ==  BluetoothProfile.A2DP){
				Log.i(TAG,"==============BluetoothProfile.A2DP:"+profile);
			}

    	}
    };

}

80,469

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧