我是一名学生,我是安卓世界的新手。
我想做的是尝试将我的智能手表连接到Arduino Nano BLE 33(因为我应该在我的smarwatch上显示Arduino的数据(。
首先,我尝试使用这个指南蓝牙低能耗,但我失败了(可能我做错了什么(。然后我读了这个蓝牙概述,我试图开发一个智能手机应用程序(因为它更容易(,我得到了这个:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button scanButton;
ListView scanListView;
ArrayList<String> stringArrayList = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;
BluetoothAdapter myAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanButton = (Button) findViewById(R.id.scan_button);
scanListView = (ListView) findViewById(R.id.scanned_list);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAdapter.startDiscovery();
}
});
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, intentFilter);
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,stringArrayList);
scanListView.setAdapter(arrayAdapter);
}
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
stringArrayList.add(device.getName());
arrayAdapter.notifyDataSetChanged();
}
}
};
}
它在我的智能手机上工作,我的智能电话检测到Arduino。现在我想在智能手表上得到同样的东西,但我不知道该更改代码。
有人能帮我吗??
如果我的要求很愚蠢,我很抱歉。
考虑磨损操作系统的代码:
public class MainActivity extends WearableActivity {
private TextView text;
private static final int REQUEST_ENABLE_BT = 1;
private BluetoothAdapter btAdapter;
private ListView scanList;
//private ArrayList<String> stringArrayList = new ArrayList<String>();
//private ArrayAdapter<String> arrayAdapter;
private Button scanButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
scanButton = (Button) findViewById(R.id.button);
// recuperiamo un riferimento al BluetoothManager
// BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
// recuperiamo un riferimento all'adapter Bluetooth
btAdapter = BluetoothAdapter.getDefaultAdapter();
// verifichiamo che Bluetooth sia attivo nel dispositivo
if (btAdapter == null || !btAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
}
// premendo il bottone inizio la ricerca dei dispositivi
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btAdapter.startDiscovery();
text.setText("Ricerca dispositivi");
}
});
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
//scanList.setAdapter(arrayAdapter);
// Enables Always-on
setAmbientEnabled();
}
// Create a BroadcastReceiver for ACTION_FOUND.
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
text.setText("Ecco il dispositivo trovato");
//stringArrayList.add(device.getName()); //prendo i nomi dei device
//String deviceHardwareAddress = device.getAddress(); // MAC address
}
else
text.setText("Nessun dispositivo trovato");
}
};
}
寄存器接收器可能不适用于WearableActivity吗?