Java 不推荐使用蓝牙低能耗设备

Java 不推荐使用蓝牙低能耗设备,java,Java,我目前正在为android开发一个应用程序,它使用蓝牙低能量连接到IOS设备,这样他们就可以互相传输数据。android设备将成为我的中心设备,而IOS设备则是外围设备。然而,由于这个应用程序是在一段时间前创建的,用于搜索设备的StarteScan方法已经被弃用,在做了一些研究之后,我了解到startScan()是我应该使用的新方法。然而,我一直在努力在我的程序中实现这个新方法。有人可以编辑我的代码来包含这个新方法吗 MainActivity.java import android.blueto

我目前正在为android开发一个应用程序,它使用蓝牙低能量连接到IOS设备,这样他们就可以互相传输数据。android设备将成为我的中心设备,而IOS设备则是外围设备。然而,由于这个应用程序是在一段时间前创建的,用于搜索设备的StarteScan方法已经被弃用,在做了一些研究之后,我了解到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);
        }

    }
}
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.BluetoothGatt;
导入android.bluetooth.BluetoothGattCallback;
导入android.bluetooth.BluetoothGattCharacteristic;
导入android.bluetooth.BluetoothGattDescriptor;
导入android.bluetooth.BluetoothGattService;
导入android.bluetooth.BluetoothManager;
导入android.bluetooth.BluetoothProfile;
导入android.content.Context;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.TextView;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
私人蓝牙适配器;
私人蓝牙gatt;
私有蓝牙特征输入特征;
私有文本视图输出视图;
私有编辑文本输入视图;
私有静态最终整数请求_ENABLE_BT=1;
BluetoothAdapter.LeScanCallback-LeScanCallback;
***公共无效接收模式(视图五){
bluetoothAdapter.startedscan(leScanCallback);
} ***
公共无效发送消息(视图v){
设置值(inputView.getText().toString());
gatt.写特征(输入特征);
}
私有最终BluetoothGattCallback gattCallback=新BluetoothGattCallback(){
@凌驾
特征变更后的公共无效(蓝牙gatt,最终蓝牙gatt特征特征){
if(getString(R.string.outputUUID).equals(characteristic.getUuid().toString()){
最终字符串值=characteristic.getStringValue(0);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
outputView.setText(值);
}
});
}
}
@凌驾
连接状态更改的公共无效(最终蓝牙gatt gatt、最终int状态、最终int新闻状态){
main activity.this.gatt=gatt;
if(newState==BluetoothProfile.STATE\u CONNECTED){
试一试{
睡眠(1500);
}捕捉(中断异常e){
e、 printStackTrace();
}
关贸总协定。发现服务();
}
}
@凌驾
发现服务上的公共无效(Bluetooth gatt,int状态){
List services=gatt.getServices();
for(BluetoothGattService服务:服务){
列表特征=service.getCharacteristics();
for(蓝牙gatt特征:特征){
if(getString(R.string.outputUUID).equals(characteristic.getUuid().toString()){
试一试{
睡眠(500);
}捕捉(中断异常e){
e、 printStackTrace();
}
gatt.setCharacteristicNotification(特征,真实);
BluetoothGattDescriptor描述符=characteristic.getDescriptors().get(0);
if(描述符!=null){
描述符.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
关贸总协定书面说明者(描述符);
}
}else if(getString(R.string.inputUUID).equals(characteristic.getUuid().toString()){
试一试{
睡眠(500);
}捕捉(中断异常e){
e、 printStackTrace();
}
输入特性=特性;
}
}
}
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputView=(TextView)findViewById(R.id.outputText);
inputView=(EditText)findViewById(R.id.inputText);
最终BluetoothManager BluetoothManager=
(BluetoothManager)getSystemService(Context.BLUETOOTH\u服务);
bluetoothAdapter=bluetoothManager.getAdapter();
if(bluetoothAdapter!=null&&!bluetoothAdapter.isEnabled()){
Intent enableBtIntent=新建
意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
}
}
活动\u 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>

更换

        bluetoothAdapter.startLeScan(leScanCallback);


请看:我尝试过使用这种新方法,但当IOS设备放入外围设备时,我找不到任何设备。不确定这是为什么安卓显示器说跳过了120帧,应用程序可能在主线程上做了太多的工作。问题是,我不确定我的代码的哪一部分会在一个单独的线程上运行。IOS设备是否在积极宣传它的存在?重写ScanCallback中的其他方法,以查看启动扫描时是否出错。
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());
   }
});