android关于连接蓝牙socket 通道

 有时候 我们需要 自动连接 app 的socket 数据通道   ,,通常情况下,我们都只是 手点 设置》蓝牙 》搜索 》绑定》连接  电话/多媒体 》   ,上面的连接 仅仅是 A2DP 以及HEADSET 的连接    如何 自动连接  socket通道?  楼主有了一个想法

代码如下:

// 检查如果蓝牙音频连接。 
		int result = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
		if(result==BluetoothProfile.STATE_CONNECTED)
		{
			 ServiceListener listener = new BluetoothProfile.ServiceListener(){
	
				@Override
				public void onServiceConnected(int profile, BluetoothProfile proxy) 
				{ 
					if (proxy != null)
					{
						List<BluetoothDevice> cd = proxy.getConnectedDevices();
				
						if (cd.size()>0) {
							BluetoothDevice t = cd.get(0);
							mDeBluetooth.connect(t);
							mConnectedDeviceName = t.getName();															

						}								
					}
				}
	
				@Override
				public void onServiceDisconnected(int profile) {

				}
				 
			 };
			bluetoothAdapter.getProfileProxy(context, listener, BluetoothProfile.HEADSET);
		}

这个方法  肯定是有弊端的    ,有高手  的话   可以介绍另一种自动连接的方法?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章