蓝牙低功耗startLeScan已被弃用

时间:2017-08-03 11:00:50

标签: java

我目前正在开发一款Android应用程序,它使用蓝牙低功耗连接到IOS设备,以便它们可以相互传输数据。 Android设备将成为我的核心,IOS设备是外围设备。但是,由于此应用程序是在不久前创建的,因此不推荐使用startLeScan方法来搜索设备,在进行一些研究后,我了解到startScan()是我应该使用的新方法。但是,我一直在努力在我的程序中实现这个新方法。有人编辑我的代码以包含新方法吗?

MainActivity.java

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.List;


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

    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="uk.ac.york.androidtoios2.MainActivity">

    <EditText
        android:id="@+id/inputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="sendMessage"
        android:text="Send Message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:onClick="receiveMode"
        android:text="Connect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/outputText"
        android:text="Message"
        android:gravity="center"
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

替换

        bluetoothAdapter.startLeScan(leScanCallback);

使用

bluetoothAdapter.getBluetoothLeScanner().startScan(new ScanCallback() {
 @Override
 public void onScanResult(int callbackType, ScanResult result) {
 super.onScanResult(callbackType, result);
 Log.i(TAG, "Remote device name: " + result.getDevice().getName());
   }
});