2012-04-08 9 views
0

私は何か悪いことを書いた場合は謝罪しますので、私は謝罪します 私のコードではデバイスを見つけるはずのエラーがいくつかあります(EclipseではOKですが、 「デバイスを探す:(それは、Bluetoothをサポートしていませんので、あなたはカントAndroid - Bluetoothデバイスエラーを発見しました

コード

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

答えて

1

は、エミュレータ上でこれを実行します。あなたは、実際のデバイス上でテストする必要があるボタンをクリックメートル。

マニフェストにBluetooth許可を含めることを忘れないでください。

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

私はこの権限を持っているが、コードがcorretly動作しません(私はボタンをクリックした研ぐbFind一部ForceQuitがemmitedさ)ペースト? – PatLas

+0

あなたがエミュレータ上でそれを実行しているあなたのエラー・スタックがあまりに – waqaslam

+0

いいえ、私はHTC Wildfire S – PatLas

関連する問題