繁体   English   中英

Android到IOS蓝牙聊天应用程序

[英]Android to IOS Bluetooth Chat Application

我目前正在尝试创建一个应用程序,该应用程序将使Android和IOS设备能够通过蓝牙相互连接,然后相互发送消息。 以下是我为此应用程序的代码

维护性

public class MainActivity extends AppCompatActivity {

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothGatt gatt;
    private BluetoothGattCharacteristic inputCharacteristic;
    private TextView outputView;
    private EditText inputView;
    private static final int REQUEST_ENABLE_BT = 1;
    BluetoothAdapter.LeScanCallback leScanCallback;

    public void receiveMode(View v) {

        bluetoothAdapter.startLeScan(leScanCallback);
    }

    public void sendMessage(View v) {
        inputCharacteristic.setValue(inputView.getText().toString());
        gatt.writeCharacteristic(inputCharacteristic);
    }


    private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {

            if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) {
                final String value = characteristic.getStringValue(0);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        outputView.setText(value);
                    }
                });

            }
        }
        @Override
        public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
            MainActivity.this.gatt = gatt;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gatt.discoverServices();
            }

        }
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            List<BluetoothGattService> services = gatt.getServices();
            for (BluetoothGattService service : services) {
                List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                for (BluetoothGattCharacteristic characteristic : characteristics) {
                    if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        gatt.setCharacteristicNotification(characteristic, true);
                        BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
                        if (descriptor != null) {
                            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            gatt.writeDescriptor(descriptor);
                        }
                    } else if (getString(R.string.inputUUID).equals(characteristic.getUuid().toString())) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        inputCharacteristic = characteristic;
                    }
                }
            }


        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        outputView = (TextView) findViewById(R.id.outputText);
        inputView = (EditText) findViewById(R.id.inputText);

        final BluetoothManager bluetoothManager =
                (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();

        if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new
                    Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

    }
}

我无法使该应用程序正常运行,因为在运行startLeScan时,我从android监视器接收到空回调。 我相信这是因为此方法已被弃用,因此不再有效。 有人可以编辑我的代码以使其使用其他方法扫描设备吗?

这是我收到的错误消息:

08-02 11:36:06.470 31188-31188 / uk.ac.york.androidtoiosble D / BluetoothAdapter:startLeScan():null 08-02 11:36:06.470 31188-31188 / uk.ac.york.androidtoiosble E / BluetoothAdapter :startLeScan:空回调

对于初学者来说,您会得到一个null回调,因为您实际上从不创建一个回调。

但是,要回答第二个问题

使用替代方法来扫描设备?

您的IDE可能会告诉您这一点,但是阅读文档后,您将看到...。

此方法已在API级别21中弃用。请改用startScan(List, ScanSettings, ScanCallback)

但是同样,您必须显式创建ScanCallback对象。

仔细阅读本文,以了解如何找到蓝牙设备

https://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM