蓝牙一:蓝牙的简单使用,包括打开蓝牙和关闭蓝牙
发布日期:2021-05-09 18:26:51 浏览次数:22 分类:精选文章

本文共 4280 字,大约阅读时间需要 14 分钟。

���������Android������������������������������������������������������������������������������������������������������������ yellow car��������������������������������������������������������������������������������������������������������������������� Android ���������������������������������������������

������������������

������������������������������������������Android������������������������������������������������������������������������

������������

������������������������������������������������������������������������������������������������������������������������������������������

������������

������������������������������OnClickListener���������������������������������������������������������������

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_on, btn_off, btn_visible, btn_list;
private BluetoothAdapter bluetoothAdapter;
private Set
pairedDevices;
private ListView lv;
private List
list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_on = (Button) findViewById(R.id.btn_on);
btn_off = (Button) findViewById(R.id.btn_off);
btn_visible = (Button) findViewById(R.id.btn_visible);
btn_list = (Button) findViewById(R.id.btn_list);
lv = (ListView) findViewById(R.id.listView1);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btn_on.setOnClickListener(this);
btn_off.setOnClickListener(this);
btn_visible.setOnClickListener(this);
btn_list.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_on:
getOpen();
break;
case R.id.btn_off:
getClose();
break;
case R.id.btn_visible:
getView();
break;
case R.id.btn_list:
getList();
break;
}
}
private void getView() {
Intent getintent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getintent, 0);
}
private void getClose() {
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), "������������", Toast.LENGTH_SHORT).show();
}
private void getList() {
pairedDevices = bluetoothAdapter.getBondedDevices();
list = new ArrayList<>();
for (BluetoothDevice bt : pairedDevices) {
list.add(bt.getName());
}
Toast.makeText(getApplicationContext(), "������������������", Toast.LENGTH_SHORT).show();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
private void getOpen() {
if (!bluetoothAdapter.isEnabled()) {
Intent turnon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnon, 0);
Toast.makeText(getApplicationContext(), "������������", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "������������������", Toast.LENGTH_SHORT).show();
}
}
}

������������������������������������������������������������������������������������������������������������������������������������������������������������Android���������������������������������������������������������������������������

��������������������������������������������������������������� wearables ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:蓝牙通信二:测试手机蓝牙当前状态
下一篇:android解决NestedScrollView和ListView冲突问题

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月12日 22时35分49秒