2012-04-08 461 views
0

我是新來的,所以我很抱歉,如果我寫的東西不好 我在我的代碼中的一些錯誤應該找一些,但設備(在Eclipse中它看起來不錯,但它顯示了一些力量,而我退出「M點擊按鈕查找設備:(Android的 - 藍牙設備發現錯誤

代碼

package com.moj.test; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class Bluetooth extends Activity{ 

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    private static final int REQUEST_ENABLE_BT = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.bluetooth); 
     Button bStart = (Button) findViewById(R.id.btbutton1); 
     Button bFind = (Button) findViewById(R.id.btbutton2); 

     bStart.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       BluetoothStart();  
      } 
     }); 

     bFind.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // Register the BroadcastReceiver 
       IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
       registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 
       mBluetoothAdapter.startDiscovery(); 


      } 
     }); 


    } 


    public void BluetoothStart() { 
     if (mBluetoothAdapter != null) { 
      if (!mBluetoothAdapter.isEnabled()) { 
       //Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_ENABLE_BT); 
      } 
     } 
    } 


    // Create a BroadcastReceiver for ACTION_FOUND 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      EditText te = (EditText) findViewById(R.id.editText1); 
      String action = intent.getAction(); 
      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       // Add the name and address to an array adapter to show in a ListView 
       te.setText(device.getName() + "\n" + device.getAddress()); 
      } 
     } 
    }; 


} 
+1

粘貼錯誤堆棧太 – waqaslam 2012-04-08 10:38:11

回答

1

你不能在模擬器上運行,是因爲它沒有支持藍牙,你需要測試它在真實設備上。

和不要忘記在清單中包含藍牙許可。

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

我有這個權限,但代碼不corretly工作(磨我按下按鈕bFind一些ForceQuit是emmited) – PatLas 2012-04-08 11:09:13

+0

你在模擬器中運行呢? – waqaslam 2012-04-08 11:23:28

+0

沒有,我'上運行它HTC野火S – PatLas 2012-04-08 12:01:06