2012-12-20 27 views
0

我做了兩項活動的申請。第一項活動導入用戶參數,第二項活動通過藍牙發送數據。我使用.ACTION_REQUEST_ENABLE啓用藍牙,如果它被禁用,但是當bt關閉時,我的apk會退出。它不起作用。任何幫助?如何啓用藍牙,當它禁用我的代碼內?

我用這個;在創建活動,啓動處理程序,並runnnable後的代碼...我測試它只是findBT和工作...

void findBT() 
{ 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if(mBluetoothAdapter == null) 
    { 
     myLabel.setText("No bluetooth adapter available"); 
    } 

    if(!mBluetoothAdapter.isEnabled()) 
    { 
     //My problem is there 
     Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBluetooth, 0); 
    } 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    if(pairedDevices.size() > 0) 
    { 
     for(BluetoothDevice device : pairedDevices) 
     { 
      if(device.getName().equals(strValue2)) 
      { 
       mmDevice = device; 
       break; 
      } 
     } 
    } 
    myLabel.setText("Bluetooth Device Found"); 
    } 
+0

你的意思是你的應用程序崩潰了「apk exit」? –

+0

hey shreya你有什麼關於藍牙API ... :) –

回答

2

此代碼來檢查您的設備已啓用藍牙或不:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
if (mBluetoothAdapter == null) { 
Toast.makeText(this.getParent(), ConfigClass.BLUETOOTH_NOT_SUPPORTED_ERROR, Toast.LENGTH_LONG); 
} 

檢查並啓用藍牙:

else if (!mBluetoothAdapter.isEnabled()) { 
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
startActivityForResult(enableBtIntent, RESULT_OK); 
+0

什麼是ConfigClass?是紅色的... – user1909897

+0

它是我的常用課程...你可以在那個地方寫你自己的消息...讓我知道如果你需要任何其他功能信息 –

3

這裏是代碼:

public class AndroidBluetooth extends Activity { 

    private static final int REQUEST_ENABLE_BT = 1; 

    /** Called when the activity is first created. */ 

    TextView stateBluetooth; 
    BluetoothAdapter bluetoothAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     stateBluetooth = (TextView)findViewById(R.id.bluetoothstate); 
     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     CheckBlueToothState(); 
    } 

    private void CheckBlueToothState(){ 
     if (bluetoothAdapter == null){ 
      stateBluetooth.setText("Bluetooth NOT support"); 
     }else{ 
      if (bluetoothAdapter.isEnabled()){ 
       if(bluetoothAdapter.isDiscovering()){ 
        stateBluetooth.setText("Bluetooth is currently in device discovery process."); 
       }else{ 
        stateBluetooth.setText("Bluetooth is Enabled."); 
       } 
      }else{ 
       stateBluetooth.setText("Bluetooth is NOT Enabled!"); 
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
      } 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     if(requestCode == REQUEST_ENABLE_BT){ 
      CheckBlueToothState(); 
     } 

    } 


} 
+0

好的。什麼是REQUEST_ENABLE_BT?我如何聲明它? – user1909897

+0

似乎是我編輯的答案... –

+0

Ok.Thanks!Work!For mine miss ActivityResult .... – user1909897