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);
		}

這個方法  肯定是有弊端的    ,有高手  的話   可以介紹另一種自動連接的方法?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章