我想用Android蓝牙API显示已绑定设备的列表,但我不知道为什么在我的设备上运行它时,此代码中没有显示toast?顺便说一下,toast将包含每个设备的名称。
package com.example.fatma.cst;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Set;
import java.util.Vector;
public class MainActivity extends AppCompatActivity {
TextView txt = null;
private static final int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
final Vector mArrayAdapter = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter==null){
Toast t=new Toast(this);
t.setText("Sorry your phone do not support Bluetooth");
t.show();
}
else
{
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if(REQUEST_ENABLE_BT!=0) {
/* bluetoothAdapter.startDiscovery();
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};*/
//IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
// registerReceiver(mReceiver, filter);
Set<BluetoothDevice> devices;
devices = bluetoothAdapter.getBondedDevices();
for (BluetoothDevice blueDevice : devices) {
Toast.makeText(this, "Device = " + blueDevice.getName(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
答案 0 :(得分:1)
这是我使用的一种方法(并且没有什么特别的)来迭代和记录或配对配对的蓝牙设备。您的代码似乎在做同样的事情。
glorot_uniform
答案 1 :(得分:0)
通过在for循环中添加Toast来尝试此代码。
$text_files_path = "C:/inetpub/wwwroot/dbs";
$command = ("diff $text_files_path/$OldDbFile $text_files_path/$NewDbFile > $text_files_path/diffFile.txt");
exec($command);
您也可以参考此Bluetooth Android Developer