2010-09-27 34 views
82

HI all,Cách bật/tắt bluetooth lập trình trong android

Tôi muốn bật/tắt bluetooth thông qua chương trình .. Tôi có mã sau đây.

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
if (!mBluetoothAdapter.isEnabled()) { 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 

Nhưng loại mã này không hoạt động trong SDK 1.5 .. Tôi có thể làm như thế trong SDK 1.5.

+0

Làm thế nào là nó không làm việc? bạn đang nhận được một lỗi? Nếu vậy thì lỗi là gì? –

+0

BluetoothAdapter hiển thị lỗi trong SDK 1.5 – user458295

Trả lời

126

mã này làm việc cho tôi ..

//Disable bluetooth 
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
if (mBluetoothAdapter.isEnabled()) { 
    mBluetoothAdapter.disable(); 
} 

Để làm việc này, bạn phải có các quyền sau đây:

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

nó thực sự làm việc cho tôi. phương pháp đơn giản để ngắt kết nối bluetooth trong các thiết bị Android. cảm ơn rất nhiều bạn thân. –

+6

nếu bạn thêm quyền BLUETOOTH_ADMIN nó hoạt động nhưng nếu không bạn cần sử dụng startActivityForResult (enableBtIntent, 0); để bật bluetooth –

+0

Cảm ơn câu trả lời hữu ích của bạn +1. chỉ cần tôi muốn thêm cho những người không biết làm thế nào để kích hoạt nó: mBluetoothAdapter.enable() –

5

Giải pháp của prijin làm việc hoàn hảo cho tôi. Nó chỉ là công bằng đề cập đến rằng hai quyền bổ sung là cần thiết:

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

Khi chúng được thêm vào, cho phép và vô hiệu hóa các công trình hoàn hảo với bộ điều hợp bluetooth mặc định.

66

Dưới đây là cách mạnh mẽ hơn một chút để làm điều này, cũng xử lý các giá trị trở lại của enable()\disable() phương pháp:

public static boolean setBluetooth(boolean enable) { 
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    boolean isEnabled = bluetoothAdapter.isEnabled(); 
    if (enable && !isEnabled) { 
     return bluetoothAdapter.enable(); 
    } 
    else if(!enable && isEnabled) { 
     return bluetoothAdapter.disable(); 
    } 
    // No need to change bluetooth state 
    return true; 
} 

Và thêm các điều khoản sau đây vào file manifest của bạn:

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

Nhưng hãy nhớ những điểm quan trọng sau:

Đây là cuộc gọi không đồng bộ: cuộc gọi sẽ trở lại ngay lập tức, và khách hàng nên lắng nghe ACTION_STATE_CHANGED để được thông báo về các thay đổi trạng thái bộ điều hợp tiếp theo. Nếu cuộc gọi này trả về true, thì trạng thái của bộ chuyển đổi sẽ chuyển đổi ngay lập tức từ STATE_OFF sang STATE_TURNING_ON, và một thời gian sau đó chuyển sang STATE_OFF hoặc STATE_ON. Nếu cuộc gọi này trả về false thì có sự cố tức thì sẽ ngăn không cho bộ điều hợp bật - chẳng hạn như Chế độ trên máy bay hoặc bộ điều hợp đã được bật.

UPDATE:

Ok, vậy làm thế nào để thực hiện bluetooth nghe ?:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     final String action = intent.getAction(); 

     if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { 
      final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 
               BluetoothAdapter.ERROR); 
      switch (state) { 
      case BluetoothAdapter.STATE_OFF: 
       // Bluetooth has been turned off; 
       break; 
      case BluetoothAdapter.STATE_TURNING_OFF: 
       // Bluetooth is turning off; 
       break; 
      case BluetoothAdapter.STATE_ON: 
       // Bluetooth has been on 
       break; 
      case BluetoothAdapter.STATE_TURNING_ON: 
       // Bluetooth is turning on 
       break; 
      } 
     } 
    } 
}; 

Và làm thế nào để đăng ký/unregister người nhận?(Trong lớp Activity của bạn)

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // ... 

    // Register for broadcasts on BluetoothAdapter state change 
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
    registerReceiver(mReceiver, filter); 
} 

@Override 
public void onStop() { 
    super.onStop(); 

    // ... 

    // Unregister broadcast listeners 
    unregisterReceiver(mReceiver); 
} 
+1

nếu bạn thêm quyền 'BLUETOOTH_ADMIN', nó sẽ hoạt động nhưng nếu bạn không cần sử dụng' startActivityForResult (enableBtIntent, 0); 'để bật bluetooth –

+1

thông tin được đánh dấu được trích dẫn từ tài liệu BluetoothAdapter, cụ thể cho phương thức enable() . –

18

Để Kích hoạt tính năng Bluetooth, bạn có thể sử dụng một trong các chức năng sau:

public void enableBT(View view){ 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (!mBluetoothAdapter.isEnabled()){ 
     Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     // The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult() 
     // implementation as the requestCode parameter. 
     int REQUEST_ENABLE_BT = 1; 
     startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT); 
     } 
    } 

Chức năng thứ hai là:

public void enableBT(View view){ 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (!mBluetoothAdapter.isEnabled()){ 
     mBluetoothAdapter.enable(); 
    } 
} 

Sự khác biệt là các chức năng đầu tiên làm cho ứng dụng yêu cầu người dùng quyền bật Bluetooth hoặc từ chối. Chức năng thứ hai làm cho ứng dụng bật Bluetooth trực tiếp.

Để Vô hiệu hóa Bluetooth sử dụng chức năng sau:

public void disableBT(View view){ 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (mBluetoothAdapter.isEnabled()){ 
     mBluetoothAdapter.disable(); 
    } 
} 

LƯU Ý/Chức năng đầu tiên cần chỉ cho phép sau để được định nghĩa trong file AndroidManifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH"/> 

Trong khi, các chức năng thứ hai và thứ ba cần các quyền sau đây:

<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
4

Tôi đã sử dụng mã bên dưới để tắt BT khi ứng dụng của tôi khởi chạy và hoạt động tốt. Không chắc chắn nếu điều này đúng cách để thực hiện điều này như google khuyến cáo không sử dụng "bluetooth.disable();" mà không có hành động người dùng rõ ràng để tắt Bluetooth.

BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); 
    bluetooth.disable(); 

Tôi chỉ sử dụng quyền dưới đây.

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

Thêm các điều khoản sau đây vào file manifest của bạn:

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

Enable sử dụng bluetooth này

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
if (!mBluetoothAdapter.isEnabled()) { 
    mBluetoothAdapter.enable(); 
}else{Toast.makeText(getApplicationContext(), "Bluetooth Al-Ready Enable", Toast.LENGTH_LONG).show();} 

Disable bluetooth sử dụng này

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
if (mBluetoothAdapter.isEnabled()) { 
    mBluetoothAdapter.disable(); 
} 
0

thử điều này:

//this method to check bluetooth is enable or not: true if enable, false is not enable 
public static boolean isBluetoothEnabled() 
    { 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (!mBluetoothAdapter.isEnabled()) { 
      // Bluetooth is not enable :) 
      return false; 
     } 
     else{ 
      return true; 
     } 

    } 

//method to enable bluetooth 
    public static void enableBluetooth(){ 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (!mBluetoothAdapter.isEnabled()) { 
      mBluetoothAdapter.enable(); 
     } 
    } 

//method to disable bluetooth 
    public static void disableBluetooth(){ 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (mBluetoothAdapter.isEnabled()) { 
      mBluetoothAdapter.disable(); 
     } 
    } 

Thêm những quyền này trong manifest

<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
Các vấn đề liên quan