2012-10-24 173 views
0

我m建立一个android蓝牙聊天应用程序,并面临一些问题。我的问题是:我无法检测范围内的可用蓝牙设备,无法显示在列表中。作为即时新到android编程无法检测到问题。请帮助我。android蓝牙聊天应用程序

我的代码是:

public class BluetoothSearchActivity extends Activity { 

    ArrayAdapter<String> btArrayAdapter; 
    BluetoothAdapter mBluetoothAdapter; 
    TextView stateBluetooth; 

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

     ImageView BluetoothSearchImageView=new ImageView(this); 
     BluetoothSearchImageView.setImageResource(R.drawable.inner1); 

     setContentView(BluetoothSearchImageView); 
     setContentView(R.layout.activity_bluetooth_search); 

     mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); 

     ListView listDevicesFound=(ListView) findViewById(R.id.myList); 

     btArrayAdapter=new ArrayAdapter<String> (BluetoothSearchActivity.this,android.R.layout.simple_list_item_1); 

     listDevicesFound.setAdapter(btArrayAdapter); 

     registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND)); 

     btArrayAdapter.clear(); 
     mBluetoothAdapter.startDiscovery(); 

    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(ActionFoundReceiver); 
    } 

    private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      String action=intent.getAction(); 
      if(BluetoothDevice.ACTION_FOUND.equals(action)) { 
       BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       btArrayAdapter.add(device.getName()+"\n"+device.getAddress()); 
       btArrayAdapter.notifyDataSetChanged(); 
      } 
     } 
    }; 
+2

你拥有所有正确的权限? – thepoosh

回答

0

你有没有设置其他设备如发现?默认情况下,大多数智能手机对配对设备都是不可见的,您需要启用可见性才能看到它。

我看到你使用API​​参考的例子,所以它应该工作。在AndroidManifest.xml文件

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

请确保您有。

除此之外,你的对象有一些奇怪的命名约定。 ActionFoundReceiver应该是actionFoundReceiver(它的对象不是一个类),但没什么大不了的。

如果您收到任何类型的错误信息,请将其发布。

编辑:您可以在Log.d(标记,字符串)中写出设备名称和地址,以便在logcat调试中读出它,消除了显示列表时出现的问题。

编辑:为Log.d

import android.util.Log; 

//to use Log.d(String, String) 
Log.d("someTag", "your text here"); 

您可以使用Log.d打印的调试信息,帮助您了解如果您的代码执行正确添加代码。我会Log.d("tag", device.getName()+" "+device.getAddress());看看你是否gettnig任何来自听众的回应。并消除你可能有任何问题,从试图把它变成一个列表等。

- 托马斯Flølo

+0

如何在我的project.please中使用Log.d(标记,字符串)告诉me.with编写代码。 – user1770346

+0

我会将我编辑的代码放入您的ActionFoundReceiver中。 – tFlolo

+0

要查看Log.d的输出,您需要在Eclipse中显示logcat窗口或从命令提示符使用ADB。 – tFlolo