Android-виджет для включения Bluetooth-модема

Я пытаюсь включить модем Bluetooth на Android Nexus 5 4.4.4 из приложения.

У кого-нибудь есть рабочий код для этого? Я видел несколько примеров использования класса android.bluetooth.BluetoothPan для этого, но я не могу заставить его работать.

С примерами я получаю следующую ошибку в logcat:

java.lang.reflect.InvocationTargetException Caused by: java.lang.NullPointerException at android.bluetooth.BluetoothPan.isTetheringOn(BluetoothPan.java:346)

Спасибо за совет.

ОБНОВЛЕНИЕ: Вот еще некоторые подробности. Я пытаюсь получить текущее состояние привязки.

Метод onCreate:

private boolean btEnabled = false;

BluetoothAdapter mBluetoothAdapter = null;
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Class<?> noparams[] = {};
Method mIsBTTetheringOn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    try {
        RetrieveWeather();
    } catch (IOException e) {
        e.printStackTrace();
    }
    CheckBluetoothState();


    Context MyContext = getApplicationContext();
    mBluetoothAdapter = getBTAdapter();
    try {
        classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
        mIsBTTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", noparams);
        BTPanCtor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
        BTPanCtor.setAccessible(true);
        BTSrvInstance = BTPanCtor.newInstance(MyContext, new BTPanServiceListener(MyContext));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }


    boolean test = IsBluetoothTetherEnabled();
    if (test){
        Log.d("Tethering", "Bluetooth Tethering is On");
    } else {
        Log.d("Tethering", "Bluetooth Tethering is Off");
    }
}



private BluetoothAdapter getBTAdapter() {
    if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
        return BluetoothAdapter.getDefaultAdapter();
    else {
        BluetoothManager bm = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        return bm.getAdapter();
    }
}


// Check whether Bluetooth tethering is enabled.
private boolean IsBluetoothTetherEnabled() {
    try {
        if(mBluetoothAdapter != null) {

            return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

Ошибка возникает в следующей строке: return (Boolean) mIsBTTetheringOn.invoke(BTSrvInstance, (Object []) noparams);

С наилучшими пожеланиями


person GMSonic    schedule 21.09.2014    source источник
comment
Добро пожаловать в сообщество! Хорошей практикой является предоставление фрагмента кода или ссылок, которые привели к этой ошибке.   -  person Valentin P.    schedule 21.09.2014
comment
Привет GMSonic, я столкнулся с той же проблемой. Нашли какое-нибудь решение для этого?   -  person Santhosh Shettigar    schedule 24.11.2014
comment
Если на ваш вопрос все еще нет ответа, проверьте это: stackoverflow.com/a/22668259/1320263   -  person Pallavi    schedule 29.10.2015