蓝牙

简单得蓝牙连接

public class MainActivity extends Activity {

    // 成员变量
    BluetoothAdapter adapter;
    private ListView lv;
    private String str;
    private TextView tv;


    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    // 找到控件
    tv = (TextView) findViewById(R.id.tv);
    // 获取蓝牙适配器
    adapter = BluetoothAdapter.getDefaultAdapter();
    // 打开蓝牙
    adapter.enable();
    // 关闭蓝牙
    adapter.disable();
    // 得到蓝牙的设备状态
    adapter.getState();     
}



    // 打开蓝牙方法
    public void turnOn(View view) {
    // 判断蓝牙是否开启
    // if (adapter.isEnabled()) {
    // Toast.makeText(MainActivity.this, "蓝牙已开启", Toast.LENGTH_SHORT)
    // .show();
    // } else {
    // // 否则调用蓝牙
    // adapter.enable();
    // }
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
    startActivity(intent);      
  }


    public void shebei(View view) {
        // 获得蓝牙配对的设备
        Object[] objects = adapter.getBondedDevices().toArray();
        // 循环取到每一个设备
        for (int i = 0; i < objects.length; i++) {
        BluetoothDevice device = (BluetoothDevice) objects[i];
        str = "配对的设备名字:|" + device.getName() + "|" + device.getAddress()+device;
        tv.setText(str);
        }
        adapter.startDiscovery();
    }


    //配对蓝牙设备
    public void peidui(View view) {
    //蓝牙发送action
    Intent intent=new Intent(Intent.ACTION_SEND);
    //设置mime类型
    intent.setType("*/*");
    //要传的文件
    File file=new File(Environment.getExternalStorageDirectory()+"/sw.jpg");
    //设置文件流
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    startActivity(intent);
    }
}

布局文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="turnOn"
            android:text="打开蓝牙" />


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="shebei"
            android:text="获取蓝牙设备" />


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="peidui"
            android:text="配对蓝牙设备" />


        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值