以编程方式启用蓝牙网络共享

Bek*_*Bek 6 android bluetooth tethering tether

我正试图在Play商店制作像"蓝牙自动网络共享"这样的应用程序.我在论坛上看到Android非常安全,并且在没有用户交互的情况下不会启用此设置.

我需要一些关于如何启用蓝牙网络共享的解释.

谢谢

小智 6

我不知道这仍然是一个问题,但我发现connect在反射调用中使用该方法是有效的.在Lorelorelore的答案中使用pmont使用的代码:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Class<?> classBluetoothPan = null;
Constructor<?> BTPanCtor = null;
Object BTSrvInstance = null;
Method mBTPanConnect;

try {
    classBluetoothPan = Class.forName("android.bluetooth.BluetoothPan");
    mBTPanConnect = classBluetoothPan.getDeclaredMethod("connect", BluetoothDevice.class);
    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();
}

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
    // Loop through paired devices
    for (BluetoothDevice device : pairedDevices) {
        try{
            mBTPanConnect.invoke(BTSrvInstance, device);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,这假设蓝牙已启用,并且您只有一个配对设备.但是使用标准(非反射)调用启用蓝牙非常简单,您只需检查要在for循环中连接的配对设备.另外,不要忘记BTPanServiceListener其他答案中的课程.

希望这可以帮助.


小智 5

上面的解决方案需要修改才能为我工作。具体来说,启用网络共享的代码需要在OnServiceConnected()方法中。另外,我在清单中设置了以下权限:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)

这是我的解决方案:

public class BluetoothTethering extends ActionBarActivity {

    Object instance = null;
    Method setTetheringOn = null;
    Method isTetheringOn = null;
    Object mutex = new Object();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_tethering);
        String sClassName = "android.bluetooth.BluetoothPan";

        try {

            Class<?> classBluetoothPan = Class.forName(sClassName);

            Constructor<?> ctor = classBluetoothPan.getDeclaredConstructor(Context.class, BluetoothProfile.ServiceListener.class);
            ctor.setAccessible(true);
            //  Set Tethering ON
            Class[] paramSet = new Class[1];
            paramSet[0] = boolean.class;

            synchronized (mutex) {
                setTetheringOn = classBluetoothPan.getDeclaredMethod("setBluetoothTethering", paramSet);
                isTetheringOn = classBluetoothPan.getDeclaredMethod("isTetheringOn", null);
                instance = ctor.newInstance(getApplicationContext(), new BTPanServiceListener(getApplicationContext()));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class BTPanServiceListener implements BluetoothProfile.ServiceListener {

        private final Context context;

        public BTPanServiceListener(final Context context) {
            this.context = context;
        }

        @Override
        public void onServiceConnected(final int profile,
                                       final BluetoothProfile proxy) {
            //Some code must be here or the compiler will optimize away this callback.

            try {
                synchronized (mutex) {
                    setTetheringOn.invoke(instance, true);
                    if ((Boolean)isTetheringOn.invoke(instance, null)) {
                        Toast.makeText(getApplicationContext(), "BT Tethering is on", Toast.LENGTH_LONG).show();
                    }
                    else {
                        Toast.makeText(getApplicationContext(), "BT Tethering is off", Toast.LENGTH_LONG).show();
                    }
                }
            }
            catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(final int profile) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)