【问题标题】:Android Bluetooth unclear安卓蓝牙不清楚
【发布时间】:2016-11-06 14:09:04
【问题描述】:

我一直在阅读找到here 的安卓蓝牙指南。至此,我明白了设备是什么,套接字是什么,但我对连接有点困惑。

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

我的主要问题是: 我是否需要创建一个全新的活动来处理蓝牙? 是否所有蓝牙类都归为一个类?

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

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    这是连接到蓝牙套接字并开始 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();
            }
    

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 2011-07-21
      • 2011-07-20
      • 2012-12-16
      相关资源
      最近更新 更多