简体   繁体   English

Android到IOS蓝牙聊天应用程序

[英]Android to IOS Bluetooth Chat Application

I am currently trying to create an application which will enable Android and IOS devices to connect to each other via Bluetooth and then send messages to one another. 我目前正在尝试创建一个应用程序,该应用程序将使Android和IOS设备能够通过蓝牙相互连接,然后相互发送消息。 Below is my code for this for the android application 以下是我为此应用程序的代码

MAINACTIVITY.JAVA 维护性

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);
        }

    }
}

I have not been able to get this application working as I am receiving a null callback from the android monitor when the startLeScan is run. 我无法使该应用程序正常运行,因为在运行startLeScan时,我从android监视器接收到空回调。 I believe this is because this method has been deprecated and therfore doesn't work anymore. 我相信这是因为此方法已被弃用,因此不再有效。 Can someone edit my code to make it use an alternative method to scan for devices? 有人可以编辑我的代码以使其使用其他方法扫描设备吗?

This is the error message I recieved: 这是我收到的错误消息:

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 callback 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:空回调

For starters, you get a null callback because you literally never created one. 对于初学者来说,您会得到一个null回调,因为您实际上从不创建一个回调。

However, to answer your second question 但是,要回答第二个问题

use an alternative method to scan for devices? 使用替代方法来扫描设备?

Your IDE is probably telling you this, but reading the documentation, you would have seen.... 您的IDE可能会告诉您这一点,但是阅读文档后,您将看到...。

This method was deprecated in API level 21. use startScan(List, ScanSettings, ScanCallback) instead. 此方法已在API级别21中弃用。请改用startScan(List, ScanSettings, ScanCallback)

But again, you must explicitly create your ScanCallback object. 但是同样,您必须显式创建ScanCallback对象。

Read over this to get an idea of how you can find Bluetooth devices 仔细阅读本文,以了解如何找到蓝牙设备

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

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

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