Пройдя почти два месяца по неверному пути, я понял, в чём была моя ошибка.Сейчас я поднимаю новую проблему, на которую не могу найти ответ: с помощью этой функции при попытке подключения к гарнитуре:
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
final BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};
Я не могу инициализировать объект mBluetoothHeadset, по какой-то причине отладчик не может войти вфункция onServiceConnected ..
Любая помощь будет оценена по-настоящему ... действительно нужен один шай
Подробнее: Введено, что произошло после того, как после перезагрузки Android Android был включен, чтобы быть включенным, решено в коде: Это код функции: Log («PM.CheckForHeadSet», «In»);
if (mBluetoothAdapter == null) {
Log("PM.CheckForHeadSet","BlueTooth adapter not found");
return "Error Bluetooth adapter";
}
switch (mBluetoothAdapter.getState()){
case BluetoothAdapter.STATE_OFF:
Log("PM.CheckForHeadSet.getState"," STATE_OFF");
mBluetoothAdapter.enable();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log("PM.CheckForHeadSet.getState","STATE_TURNING_ON");
break;
case BluetoothAdapter.STATE_ON:
Log("PM.CheckForHeadSet.getState","STATE_ON");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log("PM.CheckForHeadSet.getState","STATE_TURNING_OFF");
break;
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices, add each one to the ArrayAdapter
if (pairedDevices.size() == 1) {
for (BluetoothDevice device : pairedDevices)
if(device.getBondState() == BluetoothDevice.BOND_BONDED){
Log("PM.CheckForHeadSet Connected to:",device.getName());
}
}
Log("PM.CheckForHeadSet ServiceListener:","In");
final BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null; }
}};
if(mBluetoothHeadset == null)
Log("PM.CheckForHeadSet","mBluetoothHeadset = null");
else
Log("PM.CheckForHeadSet","mBluetoothHeadset = " + mBluetoothHeadset.toString());
if(context == null)
Log("PM.CheckForHeadSet","context = null");
else
Log("PM.CheckForHeadSet","context = " + context.toString());
if(mProfileListener == null)
Log("PM.CheckForHeadSet","mProfileListener = null");
else
Log("PM.CheckForHeadSet","mProfileListener = " + mProfileListener.toString());
if(mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET) == true)
Log("PM.CheckForHeadSet.getProfileProxy","true");
else
Log("PM.CheckForHeadSet.getProfileProxy","false");
Log("PM.CheckForHeadSet","Out");
return "Set Headset";
Если я размещаю GetProfileProxy над новым ProfileListener (как в примере с документом |), переменная mProfileListener по-прежнему равна нулю, а getProfileProxy возвращает false
И это logcat:
03-12 10:09:49.906: D/SpySitter(4205): PM.CheckForHeadSet-In
03-12 10:09:50.968: D/dalvikvm(4205): threadid=1: still suspended after undo (sc=1 dc=1)
03-12 10:09:59.453: D/SpySitter(4205): PM.CheckForHeadSet.getState-STATE_ON
03-12 10:10:02.640: D/SpySitter(4205): PM.CheckForHeadSet Connected to:-Motorola H790
03-12 10:10:04.226: D/SpySitter(4205): PM.CheckForHeadSet ServiceListener:-In
03-12 10:10:13.945: D/SpySitter(4205): PM.CheckForHeadSet-mBluetoothHeadset = null
03-12 10:10:17.984: D/SpySitter(4205): PM.CheckForHeadSet-context = android.app.Application@408472a0
03-12 10:10:21.820: D/SpySitter(4205): PM.CheckForHeadSet-mProfileListener = com.example.HelloForm.Tools$1@40894d00
03-12 10:10:28.796: D/SpySitter(4205): PM.CheckForHeadSet.getProfileProxy-true
03-12 10:10:31.226: D/SpySitter(4205): PM.CheckForHeadSet-Out