藍牙鏈接音響問題(android電視)

最近老大讓我開發電視的藍牙,因爲android電視的藍牙不穩定和設計上的各類各樣的要求,須要在原有的基礎上作一些更改,中間遇到了各類問題,在此總結一下。html

咱們首先要獲取blueToothAdapter的對象    android

轉發:http://www.cnblogs.com/hyylog/p/5796711.html;ide

mBluetoothAdapter = bluetoothManager.getAdapter();spa

一、藍牙的打開:設計

   mBluetoothAdapter.enable();htm

二、藍牙關閉:對象

   mBluetoothAdapter.disenable();blog

三、藍牙是否可見:開發

   這個地方系統隱藏了其方法,咱們就要用萬能公式(反射)來實現:rem

    可見:

public static void setDiscoverableTimeout(int timeout) {
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
try {
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
setDiscoverableTimeout.setAccessible(true);
Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);
setScanMode.setAccessible(true);
setDiscoverableTimeout.invoke(adapter, timeout);
setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,timeout);
} catch (Exception e) {
e.printStackTrace();
}
}

當timeout設置爲0的時候爲永久可見

  關閉:

public static void closeDiscoverableTimeout() {
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
try {
Method setDiscoverableTimeout = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
setDiscoverableTimeout.setAccessible(true);
Method setScanMode =BluetoothAdapter.class.getMethod("setScanMode", int.class,int.class);
setScanMode.setAccessible(true);

setDiscoverableTimeout.invoke(adapter, 1);
setScanMode.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE,1);
} catch (Exception e) {
e.printStackTrace();
}
}

四、藍牙搜索,中止:

   開啓搜索:mBluetoothAdapter.startDiscovery();

  中止搜索:mBluetoothAdapter.cancelDiscovery();

五、藍牙的配對、取消配對:

 配對:

static public boolean pair(String strAddr, byte[] strPsw) {
boolean result = false;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

bluetoothAdapter.cancelDiscovery();

if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
}

BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);

if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
try {
Log.d("mylog", "NOT BOND_BONDED");
boolean flag1 = ClsUtils.setPin(device.getClass(), device,
strPsw);
boolean flag2 = ClsUtils.createBond(device.getClass(), device);
// remoteDevice = device;

result = true;

} catch (Exception e) {
// TODO Auto-generated catch block

Log.d("mylog", "setPiN failed!");
e.printStackTrace();
} //

} else {
Log.d("mylog", "HAS BOND_BONDED");
try {
ClsUtils.removeBond(device.getClass(), device);
// ClsUtils.createBond(device.getClass(), device);
boolean flag1 = ClsUtils.setPin(device.getClass(), device,
strPsw);
boolean flag2 = ClsUtils.createBond(device.getClass(), device);

result = true;

} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("mylog", "setPiN failed!");
e.printStackTrace();
}
}
return result;
}

取消配對:

static public boolean removeBond(Class<? extends BluetoothDevice> btClass,
BluetoothDevice btDevice) throws Exception {
Method removeBondMethod = btClass.getMethod("removeBond");
Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
return returnValue.booleanValue();
}

六、藍牙的鏈接:

 音響的鏈接主要是BluetoothA2dp協議,BluetoothHeadset協議主要用於耳機的,可是在實際的操做過程當中若是隻鏈接BluetoothA2dp對於廣播的接受是不正常的且有時候也是不發聲的,因此這兩個協議要一塊兒鏈接

首先要啓動這兩個協議的服務監聽:

private BluetoothProfile.ServiceListener mA2dpProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothA2dp = (BluetoothA2dp) proxy;
try {
if(realConnect)
ClsUtils.connect(mBluetoothA2dp.getClass(),mBluetoothA2dp, mDevice);
} catch (Exception e) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothA2dp);
e.printStackTrace();
}
}
}

public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.A2DP) {
mBluetoothA2dp = null;
}
}
};

/**
* @Fields mHeadsetProfileListener : BluetoothHeadset服務監聽器
*/
private BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
try {
if(realConnect)
ClsUtils.connect(mBluetoothHeadset.getClass(),mBluetoothHeadset, mDevice);
} catch (Exception e) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
e.printStackTrace();
}
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};

鏈接:

mBluetoothAdapter.getProfileProxy(mContext, mA2dpProfileListener, BluetoothProfile.A2DP);
mBluetoothAdapter.getProfileProxy(mContext, mHeadsetProfileListener, BluetoothProfile.HEADSET);

斷開:

@Override
public void disconnectBlueTooth(BluetoothDevice device) {
mDevice = device;
if(mBluetoothA2dp!=null){
try {
ClsUtils.disconnect(mBluetoothA2dp.getClass(),mBluetoothA2dp, device);
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP, mBluetoothA2dp);
} catch (Exception e) {
e.printStackTrace();
}
}
if(mBluetoothHeadset!=null){
try {
ClsUtils.disconnect(mBluetoothHeadset.getClass(),mBluetoothHeadset, device);
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

其次得到A2dp和headset對象也用上述的監聽方法

七、每次打開藍牙獲取已經鏈接的藍牙設備:

  

int a2dp = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP); int headset = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET); int health = bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEALTH); int flag = -1; if (a2dp == BluetoothProfile.STATE_CONNECTED) { flag = a2dp; } else if (headset == BluetoothProfile.STATE_CONNECTED) { flag = headset; } else if (health == BluetoothProfile.STATE_CONNECTED) { flag = health; } if (flag != -1) { bluetoothAdapter.getProfileProxy(mcContext, new ServiceListener() { @Override public void onServiceDisconnected(int profile) { } @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { List<BluetoothDevice> mDevices = proxy.getConnectedDevices(); } }, flag); }else{ listener.disConnected(); }

相關文章
相關標籤/搜索