0

我扫描设备,如果它在范围内或使用我的 android 设备启用,我想连接到另一台设备“HC-07”。我尝试了一些但未连接...有什么帮助吗?我使用 uuid 串行连接到另一个蓝牙设备...

 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)) {
             device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);//find rssi dBm

            int pair_state = device.getBondState();

            if(device.getName()=="HC-07"){
                try {
                openBT();
                Toast.makeText(getBaseContext(),"HC-07 found", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                    }
                }

            if (pair_state == 12){ pair_str="Paired";

            btArrayAdapter.add("Dev name: "+ device.getName() + "\n" + "Mac: " + device.getAddress() + "\n" + "Signal RSSI: " + rssi + "dBm" + "\n"+pair_str);

            }

            btArrayAdapter.notifyDataSetChanged();

        }
    }};

     void openBT() throws IOException
    {
        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
        mmSocket = device.createRfcommSocketToServiceRecord(uuid);       
        mmSocket.connect();
        mmOutputStream = mmSocket.getOutputStream();
    }
4

1 回答 1

0

您是否仔细查看了提供的 BluetoothChat 示例?这将向您展示如何扫描和连接到蓝牙设备以及如何使用 API 来执行此操作。 http://developer.android.com/resources/samples/BluetoothChat/index.html

检查这个例子:

蓝牙基础

http://developer.android.com/resources/samples/BluetoothChat/index.html

Android蓝牙编程的代码示例

http://marakana.com/forums/android/examples/50.html

于 2013-02-14T07:28:01.540 回答