2012-04-20 46 views

回答

5

您可以通过使用BluetoothAdapter做到这一点:

BluetoothAdapter.getDefaultAdapter().disable(); 
    BluetoothAdapter.getDefaultAdapter().enable(); 

而且增加的部份权限来体现:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
+0

现在这也提示权限对话框开启BT。 – CoDe 2015-10-29 12:32:51

0

要启用/禁用蓝牙编程无需用户交互,请使用以下代码:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
if (!mBluetoothAdapter.isEnabled()) { //Disable it if enabled 
    Intent localIntent; 
    localIntent = new Intent(); 
    localIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
    localIntent.addCategory("android.intent.category.ALTERNATIVE"); 
    localIntent.setData(Uri.parse("4")); 
    getBroadcast(paramContext, 0, localIntent, 0).send(); 
} else { //Enable if disabled 
    Intent localIntent; 
    localIntent = new Intent(); 
    localIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
    localIntent.addCategory("android.intent.category.ALTERNATIVE"); 
    localIntent.setData(Uri.parse("4")); 
    getBroadcast(paramContext, 0, localIntent, 0).send(); 
} 

而在你的Manifest.xml文件:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> 
0

是的。

private static BluetoothAdapter getBA(){ 
    return BluetoothAdapter.getDefaultAdapter(); 
} 

/*Methode startet das Bluetooth Modul 
*/ 
public static void BT_ON(){ 
    BA = getBA(); 
    BA.enable(); 
} 

public static void BT_STATUS(){ 
    BA = getBA(); 
} 
+1

它仍然在我的Android棉花糖上工作。然而,我的目标是API 22,以避免权限对话框,除了它仍然要求允许或拒绝蓝牙功能外,其它功能都可以正常工作。 – zeeshan 2017-03-06 15:26:33

相关问题