发现蓝牙样本

问题描述 投票:0回答:1

我正在学习蓝牙应用程序和我遇到的第一个样本,似乎有很好的文档,但我不能为我的生活让它重现“搜索设备”部分,任何帮助将不胜感激。

 public class MainActivity extends AppCompatActivity {

private Button onBtn, offBtn, listBtn, findBtn;
private TextView text;
private ListView myListView;

// Bluetooth global variables
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter myBluetoothAdapter;
public Set<BluetoothDevice> pairedDevices;
private ArrayAdapter<String> BTArrayAdapter;
@Override


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

        myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if(myBluetoothAdapter == null) {
            Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth",
                    Toast.LENGTH_LONG).show();
        }
        else
        {

            text = (TextView) findViewById(R.id.text);
            onBtn = (Button)findViewById(R.id.turnOn);
            offBtn = (Button)findViewById(R.id.turnOff);
            listBtn = (Button)findViewById(R.id.paired);
            findBtn = (Button)findViewById(R.id.search);

            myListView = (ListView)findViewById(R.id.listView1);

            // create the arrayAdapter that contains the bluetooth d evices, and set it to the ListView
            BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
            myListView.setAdapter(BTArrayAdapter);

            myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String selectedFromList =(String) (myListView.getItemAtPosition(i));
                    Toast.makeText(getApplicationContext(),"Bluetooth remote device : " + selectedFromList,
                            Toast.LENGTH_LONG).show();
                }
            });



        }

    } // onCreate


public void on(View view){
    if (!myBluetoothAdapter.isEnabled()) {
        Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(turnOnIntent, REQUEST_ENABLE_BT);

        Toast.makeText(getApplicationContext(),"Bluetooth turned on" ,
                Toast.LENGTH_LONG).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Bluetooth is already on",
                Toast.LENGTH_LONG).show();
    }
}

public void off(View view){
    myBluetoothAdapter.disable();
    text.setText("Status: Disconnected");

    Toast.makeText(getApplicationContext(),"Bluetooth turned off",
            Toast.LENGTH_LONG).show();
}

public void list(View view){
    // get paired devices
    pairedDevices = myBluetoothAdapter.getBondedDevices();

    // put it's one to the adapter
    for(BluetoothDevice device : pairedDevices)
        BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());

    Toast.makeText(getApplicationContext(),"Show Paired Devices",
            Toast.LENGTH_SHORT).show();

}

final BroadcastReceiver bReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // add the name and the MAC address of the object to the arrayAdapter
            BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            BTArrayAdapter.notifyDataSetChanged();
        }
    }
};

public void find(View view) {
    if (myBluetoothAdapter.isDiscovering()) {
        // the button is pressed when it discovers, so cancel the discovery
        Toast.makeText(getApplicationContext(),"Bluetooth cancelled discovery" ,
                Toast.LENGTH_LONG).show();
        myBluetoothAdapter.cancelDiscovery();
    } else {
        BTArrayAdapter.clear();
        Toast.makeText(getApplicationContext(),"Bluetooth discovery started" ,
                Toast.LENGTH_LONG).show();

        myBluetoothAdapter.startDiscovery();

        registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    }

}

@Override
protected void onDestroy() {
    super.onDestroy();
}

}

使用Oneplus3和2进行测试,除非已经相互配对,否则任何设备都无法找到其他设备。我想看看我是否可以搜索并让设备填充到已配对的设备列表中。

如果还有什么需要我让我知道,希望你不介意花时间来帮助我!

java android bluetooth
1个回答
0
投票

您需要向Manifest.xml添加以下权限之一才能找到设备:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

要么

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

如果您正在使用API​​ 23及更高版本,则必须确保已授予此权限,因为它是危险级别权限。

从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时

请参阅this guide以获取许可。

© www.soinside.com 2019 - 2024. All rights reserved.