[英]How connect to a device using the name device?
我必須在 android 中創建一個通過藍牙連接到設備的應用程序。 該設備已設置名稱“1111”,在我的 android 應用程序中,我有四個 editText 來輸入數字。 通過單擊“collega”按鈕,我希望應用程序直接連接到設備。
我在按鈕上創建了一個點擊監聽器,首先檢查藍牙是否被激活,然后嘗試使用 load() 函數連接它。 load 函數會查找附近名為“1111”的設備,但之后我不知道如何連接它。
final Button collega = (Button) findViewById(R.id.button2);
collega.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
if (!btAdapter.isEnabled())
{ Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, BLUETOOTH_ON); }
else
load(); }
});
}
private void load() {
final EditText uno=(EditText)findViewById(R.id.numero1);
final EditText due=(EditText)findViewById(R.id.numero2);
final EditText tre=(EditText)findViewById(R.id.numero3);
final EditText quattro=(EditText)findViewById(R.id.numero4);
String deviceNumber=uno.getText().toString()+due.getText().toString()+tre.getText().toString()+quattro.getText().toString();
TextView textView = (TextView) findViewById(R.id.simpleTextView);
textView.setText(deviceNumber);
devices = btAdapter.getBondedDevices();
adapter.clear();
for(BluetoothDevice bt : devices)
{
if(bt.getName()==deviceNumber){
}
}
}
我在網上搜索,但只找到了如何創建要連接的設備列表。 有人可以如何創建連接以及如何傳輸數據嗎?
您可以創建一個線程來連接藍牙套接字。
一個簡單的例子可以在這里找到: https : //developer.android.com/guide/topics/connectivity/bluetooth#ConnectAsAClient
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.