Android蓝牙不清楚

时间:2016-05-06 08:15:30

标签: android bluetooth

我一直在阅读找到here的安卓蓝牙指南。在这一点上,我理解设备是什么,插座是什么,但我对连接有点困惑。

我正在尝试使应用程序成为服务器端侦听器,它只接受第一个传入连接请求,然后随时间向其发送数据。关于如何做到这一点的说明看起来非常复杂,我想知道是否有人能告诉我最简单的方法。

我的主要问题是: 我是否需要创建一个全新的活动来处理蓝牙? 所有蓝牙课程都属于一个班级吗?

抱歉所有问题,只是想在这里学习

1 个答案:

答案 0 :(得分:0)

以下是连接蓝牙套接字并启动RFComm传输的示例,我使用调试来查找设备的UUID。您不需要单独的类,但我创建了一个单独的线程来始终运行以查找通过连接发送的数据。

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothSocket mRf, ml2;
        if (mBluetoothAdapter != null) {
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
            if (mBluetoothAdapter.isEnabled()) {
                BlueHelper client = new BlueHelper();
                Af1000Computer = client.findDevice(mBluetoothAdapter);
                if (Af1000Computer != null) {
                    try{

                        ParcelUuid[] uuids = Af1000Computer.getUuids();


                        mRf = Af1000Computer.createInsecureRfcommSocketToServiceRecord(UUID.fromString("0000110e-0000-1000-8000-00805f9b34fb"));
                        //mRf = Af1000Computer.crea
                        //ml2 = new BluetoothSocket(BluetoothSocket.TYPE_L2CAP, -1, true, true, this, 2, null);
                        try {
                            mRf.connect();
                        }catch (Exception e){
                            AlertDialog.Builder builder = new AlertDialog.Builder(this);
                            builder.setMessage("Error: " + e.getLocalizedMessage())
                                    .setTitle("Error in creating connection!");
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                        if (mRf.isConnected()){
                            fab.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.greenaf)));
                            connected = true;
                            slow.setEnabled(true);
                            harvest.setEnabled(true);
                            fast.setEnabled(true);
                            reverse.setEnabled(true);
                            thread = new RfcommThread(mRf);
                            thread.run();
                        }
                       // RfcommThread thread = new RfcommThread(mBluetoothSocket);

                        //View v = findViewById(R.id.seekBar1);
                        //thread.run(1, 2, 3);


                    }catch (Exception e){
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setMessage("Error: " + e.getLocalizedMessage())
                                .setTitle("Error in creating comm thread!");
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                }
                if (Af1000Computer == null) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Cannot find the AF1000 Harvester device! \nHave you connected to the bluetooth device?")
                            .setTitle("Error in Pairing");
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        }
        if (mBluetoothAdapter == null) {
            // Device does not support BlueHelper
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("This device does not have a bluetooth adapter!")
                    .setTitle("Error in connecting");
            AlertDialog dialog = builder.create();
            dialog.show();
        }