android连接到蓝牙 [英] android connect to bluetooth

查看:59
本文介绍了android连接到蓝牙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我有这个代码我进入 http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient [ ^ ]以发现由按钮触发的设备。但每当我运行它时,程序总会弹出一条消息强制关闭它。我不知道为什么。请帮帮我..这里是我的代码..谢谢!

Hi All,


I have this code i get in http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient[^] to discover devices that is triggered by a button. But whenever i run it, the program always pop-up a message to force close it. i don''t know why. please help me.. here''s is my code.. Thanks!

public void checkIfBluetoothOpen(View view) {

       BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
       if (mBluetoothAdapter == null) {
           // Device does not support Bluetooth
           AlertDialog.Builder builder=new AlertDialog.Builder(this);
           builder
           .setIcon(android.R.drawable.ic_dialog_alert)
           .setTitle("Sorry!")
           .setMessage("Your device does not support bluetooth.")
           .setPositiveButton("OK", null)
           .show();
       }
       else
       {
           if (!mBluetoothAdapter.isEnabled()) {

               // Bluetooth is close
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                   startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
           }

           Set<bluetoothdevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
           // If there are paired devices
           if (pairedDevices.size() > 0) {
               // Loop through paired devices
               for (BluetoothDevice device : pairedDevices) {
                   // Add the name and address to an array adapter to show in a ListView
                   mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
               }
           }
           // Create a BroadcastReceiver for ACTION_FOUND
           final BroadcastReceiver mReceiver = new BroadcastReceiver() {
               public void onReceive(Context context, Intent intent) {
                   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
                       mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                   }
               }
           };
           // Register the BroadcastReceiver
           IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
           registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

           Intent discoverableIntent = new
           Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
           discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
           startActivity(discoverableIntent);
       }
   }</bluetoothdevice>

推荐答案

通常这样的错误是由不适当的清单文件引起的。请在文档顶部查看设置权限。



Generally an error like that is caused by inappropriate manifest file. Please check at the top of the document about setting permission.

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







如果您已经完成并仍然收到错误,则表示您的模拟器没有正确设置使用您的系统的蓝牙。您只需调试应用程序,将.apk复制到手机/标签中的bin文件夹中并在那里安装即可。在真实设备中测试应用程序。




If you have done that and still getting the error, it means that your emulator is not setup properly to use your System''s bluetooth. You can just debug the App, copy the .apk in bin folder in your phone/tab and install it there. Test the app in real device.


这篇关于android连接到蓝牙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆