블루투스 앱에 대해 배우고 있으며, 필자가 만난 첫 번째 샘플은 문서화가 잘되어있는 것처럼 보입니다.하지만 필자의 삶은 "검색 장치"부분을 재현 할 수 없으며, 대단히 감사하겠습니다. Oneplus3 &이이 테스트를하고, 이미 서로 짝을하지 않는 한 어느 장치가 다른 사람을 찾을 수 있습니다 사용블루투스 샘플 검색하기
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();
}
}
. 페어링 된 장치가 이미 수행하는 것처럼 목록에서 채울 장치를 검색하고 가져올 수 있는지 확인하고 싶습니다.
나에게 알려 주어야 할 것이 있다면,이 시간을내어 도와 주시길 바랍니다. 당신은 API (23)에 노력하고 높게 이를 보장해야하는 경우
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
또는
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
:
https://github.com/googlesamples/android-BluetoothChat와 같은 알려진 블루투스 샘플을 사용하여 기기가 아닌 것을 알 수 있도록하는 것이 좋습니다. –
감사합니다. 확인해 보겠습니다. 브로드 캐스트 수신기 또는 일부 누락 된 문제가 없는지 확인하고 싶습니다. 근처에있는 모든 블루투스 장치를 찾지 못할 것입니다. –