开发者日记(2016年01月06日17:39:15):关于android bluetooth 学习:

来源:互联网 发布:电脑解压缩软件 编辑:程序博客网 时间:2024/05/16 10:25

开发者日记(2016年01月06日17:39:15):

关于android bluetooth 学习:

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();                if (bluetoothAdapter == null)//判断是否有蓝牙设备                {                    Toast.makeText(MainActivity.this, "对不起,您的手机没有蓝牙设备", Toast.LENGTH_SHORT).show();                }                else//如果拥有蓝牙设备,继续进行                {                    //如果蓝牙设备不可用,创建intent对象,该对象用于启动一个activity,提示用户启动蓝牙适配器                    if (!bluetoothAdapter.isEnabled())                    {                        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);                        startActivity(intent);                    }                    //得到所有已经配对的蓝牙适配器对象                    Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();                    if (devices.size() > 0)                    {                        /*for (Iterator iterator = devices.iterator(); iterator.hasNext(); )                        {                            //得到配对的蓝牙适配器对象                            BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();                            //得到远程蓝牙的设备地址                            textView.append("\n" + bluetoothDevice.getName() + ":" + bluetoothDevice.getAddress());                        }*/                        Iterator iterator=devices.iterator();                        while(iterator.hasNext())                        {                            //得到配对的蓝牙适配器对象                            BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();                            //得到远程蓝牙的设备地址                            textView.append("\n" + bluetoothDevice.getName() + ":" + bluetoothDevice.getAddress());                        }                        String s=devices.toString();                        Log.i("tag",s);                    }                }
Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();

1.BluetoothAdapter 蓝牙适配器
cancelDiscovery()取消发现,正在搜索其他蓝牙设备时,调用此方法便不再搜索。
disable()关闭蓝牙。
enable()打开蓝牙,此方法无提示,如需提示,用以下代码

if (!bluetoothAdapter.isEnabled())                    {                        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);                        startActivity(intent);                    }

getAddress()得到蓝牙地址
getName()得到蓝牙名称
getDefaultAdapter()获取默认BluetoothAdapter。(只用这一种方法获取BluetoothAdapter)
getRemoteDevice()根据蓝牙地址获取远程蓝牙设备
getState()获取本地蓝牙适配器当前状态
isDiscovering()判断是否在寻找蓝牙设备,如果是返回true
isEnabled()判断蓝牙是否打开,如果是返回true,否为false
startDiscovery()开始搜索
listenUsingInsecureRfcommWithServiceRecord()根据名称和UUID创建并返回BluetoothServerSocket。

2.BluetoothDevice 蓝牙设备
createRfcommSocketToServiceRecord()根据UUID创建并返回一个BluetoothSocket。

通过getBondedDevices()得到一个Set集合。
注:辨析Set,Map,List区别
这个集合仅仅只是已经配对的集合,并非尚未配对的蓝牙。

Iterator iterator=devices.iterator();

将devices这个集合返回到一个 Iterator 的迭代器中,
同时也可以返回到一个String中。
while(iterator.hasNext())类似于数据库中的结果集合resultset.next()
但是不同的是iterator的hasNext()是用来判断是否还有值,next()是用来返回下一个元素的。

接口 Iterator
专门的迭代输出接口,将元素一个个进行判断,用hasNext() 判断是否有值,用next()方法把元素取出。
hasNext()
如果仍有元素可以迭代,则返回 true。
next()
返回迭代的下一个元素。
经验的总结是只要碰到了集合输出的操作,就一定要使用Iterator接口,因为这是最标准的做法。
接口 ResultSet
表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
ResultSet 对象具有指向其当前数据行的光标。最初,光标被置于第一行之前。next 方法将光标移动到下一行;因为该方法在 ResultSet 对象没有下一行时返回 false,所以可以在 while 循环中使用它来迭代结果集。
这个是用做数据库操作的。
所以两者是没有必要比较的,用的地方不一样,只要会熟练应用就够用了。

0 0
原创粉丝点击