蓝牙知识点

package com.example.syd.testbluetoothadapter;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


/**
 *
 * 1.蓝牙的当前状态包括了BluetoothAdapter.STATE_ON,BluetoothAdapter.STATE_OFF,
 * BluetoothAdapter.STATE_TURNING_ON,BluetoothAdapter.STATE_TURNING_OFF
 *
 *
 * 2.蓝牙的绑定状态包括了 BluetoothDevice.BOND_BONDED, BluetoothDevice.BOND_BONDING, BluetoothDevice.BOND_NONE
 *
 *
 * 3.蓝牙的扫描状态包括了BluetoothAdapter.SCAN_MODE_CONNECTABLE,BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE
 *
 * 4.蓝牙的状态监听,请见代码中的BroadCastReceive
 *
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    BluetoothAdapter bluetoothAdapter;
    private int REQUEST_CODE = 1;
    Button btn_enabel;
    Button btn_disable;
    Button btn_is_opened;
    Button btn_visiable;
    Button btn_startdiscovery;
    Button btn_bonded;
    ListView lv_bonded_devices;
    ListView lv_discovery_devices;
    ArrayAdapter<String> adapterBondedDevices ;
    ArrayAdapter<String> adapterDiscoveriedDevices ;
    List<String> datasetBondedDevices = new ArrayList<String>();
    List<String> datasetDiscoveriesDevices = new ArrayList<String>();
    MyBondedDevicesAdapter myBondedDevicesAdapter;
    MyDiscoveriedDevicesAdapter myDiscoveriedDevicesAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initView();
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(new MyBlueToothReceive(), intentFilter);
    }

    private void initView() {

        btn_bonded = (Button) findViewById(R.id.btn_bonded);
        btn_bonded.setOnClickListener(this);
        btn_disable = (Button) findViewById(R.id.btn_disable);
        btn_disable.setOnClickListener(this);
        btn_enabel = (Button) findViewById(R.id.btn_enabel);
        btn_enabel.setOnClickListener(this);
        btn_is_opened = (Button) findViewById(R.id.btn_is_opened);
        btn_is_opened.setOnClickListener(this);
        btn_startdiscovery = (Button) findViewById(R.id.btn_startdiscovery);
        btn_startdiscovery.setOnClickListener(this);
        btn_visiable = (Button) findViewById(R.id.btn_visiable);
        btn_visiable.setOnClickListener(this);
        lv_bonded_devices = (ListView) findViewById(R.id.lv_bonded_devices);
        lv_discovery_devices = (ListView) findViewById(R.id.lv_discovery_device);
        myBondedDevicesAdapter = new MyBondedDevicesAdapter();
        myDiscoveriedDevicesAdapter = new MyDiscoveriedDevicesAdapter();
        lv_bonded_devices.setAdapter(myBondedDevicesAdapter);
        lv_discovery_devices.setAdapter(myDiscoveriedDevicesAdapter);
        lv_discovery_devices.setOnItemClickListener(this);
        lv_bonded_devices.setOnItemClickListener(this);


    }

    /**
     * 关闭蓝牙
     */
    public void closeBlueTooth(){
        bluetoothAdapter.disable();
    }

    /**
     * 蓝牙是否可用
     */
    public void isBlueToothEnable() {
        boolean enabled = bluetoothAdapter.isEnabled();
        if (enabled == true) showToash("蓝牙已经打开");
        else showToash(" 蓝牙未打开");
    }


    /**
     * 打开蓝牙
     */
    public void openBlueTooth(){
        Intent intent = new Intent();
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, REQUEST_CODE);
    }


    /**
     * 设备是否被其它设备发现
     */

    public void blueToothVisiabel() {
        Intent intent = new Intent();
        intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 600);
        startActivity(intent);
    }

    /**
     * 开始扫描其它设备
     */
    public void startDiscoveryBluetooth() {
        bluetoothAdapter.startDiscovery();
    }


    /**
     * 查看绑定了哪些蓝牙设备
     */
    public  void  getBondedBlueToothDevices() {
        Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
        Iterator<BluetoothDevice> iterator = bondedDevices.iterator();
        BluetoothDevice bluetoothDevice = null;
        while (iterator.hasNext()) {
            bluetoothDevice = iterator.next();
            datasetBondedDevices.add(bluetoothDevice.getName());
            if (bluetoothDevice != null)
                showToash(bluetoothDevice.getName());
        }
        myBondedDevicesAdapter.notifyDataSetChanged();

    }




    /**
     * 注册蓝牙的各种广播事件
     */

    class  MyBlueToothReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1) == BluetoothAdapter.STATE_ON) {
                    showToash("蓝牙状态为打开");
                }
                if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1) == BluetoothAdapter.STATE_OFF) {
                    showToash("蓝牙状态为关闭");
                }
                if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1) == BluetoothAdapter.STATE_TURNING_ON ) {
                    showToash("蓝牙状态正在打开");
                }
                if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1) == BluetoothAdapter.STATE_TURNING_OFF) {
                    showToash("蓝牙状态正在关闭");
                }
            }
            if (intent.getAction() == BluetoothAdapter.ACTION_DISCOVERY_STARTED) {
                showToash("开始扫描");
            }
            if (intent.getAction() == bluetoothAdapter.ACTION_DISCOVERY_FINISHED) {
                showToash("扫描结束");
            }
            if (intent.getAction() == BluetoothDevice.ACTION_FOUND) {
                BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                datasetDiscoveriesDevices.add(bluetoothDevice.getName());
                myDiscoveriedDevicesAdapter.notifyDataSetChanged();
                showToash("找到了设备" + bluetoothDevice.getName());
            }
            if (intent.getAction() == BluetoothAdapter.ACTION_SCAN_MODE_CHANGED) {
                if (intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE ,-1) == BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
                    showToash("只是可用扫描其它蓝牙设备");
                }
                if (intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,-1) == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE ) {
                    showToash("即可用扫描其它设备也是可用被其它设备扫描");
                }
            }
        }
    }




    public void showToash(String string) {
        Toast.makeText(this, string,Toast.LENGTH_SHORT).show();
    }



    /**
     * 显示蓝牙打开结果
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE)
            if (REQUEST_CODE == RESULT_CANCELED) {
                showToash("您选择了未打开蓝牙");
            }else if (REQUEST_CODE == RESULT_OK) {
                showToash("您选择了打开蓝牙");
            }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_bonded: {
                getBondedBlueToothDevices();
                break;
            }
            case R.id.btn_disable: {
                closeBlueTooth();
                break;
            }
            case R.id.btn_enabel:
            {
                openBlueTooth();
                break;
            }
            case R.id.btn_is_opened:
            {
                isBlueToothEnable();
                break;
            }
            case R.id.btn_startdiscovery:
            {
                startDiscoveryBluetooth();
                break;
            }
            case R.id.btn_visiable:
            {
                blueToothVisiabel();
                break;
            }

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        showToash("您点击的是第 " + position  + "菜单项");
    }


    /**
     * 已经绑定的设备listview的适配器
     *
     */

    class MyBondedDevicesAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datasetBondedDevices.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
               convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_bonded,null);

            }
            TextView textView1 = (TextView) convertView.findViewById(R.id.tv_bonded_item);
            textView1.setText(datasetBondedDevices.get(position));
            return  convertView;
        }
    }


    /**
     * 蓝牙扫描过程中发现的设备,适配器
     */



    class MyDiscoveriedDevicesAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datasetDiscoveriesDevices.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_discoveried,null);

            }
            TextView textView1 = (TextView) convertView.findViewById(R.id.tv_discoveried_item);
            textView1.setText(datasetDiscoveriesDevices.get(position));
            return  convertView;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值