{"id":1021,"date":"2017-04-01T14:53:32","date_gmt":"2017-04-01T06:53:32","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1021"},"modified":"2017-04-01T18:35:15","modified_gmt":"2017-04-01T10:35:15","slug":"android%e5%bc%80%e5%8f%91%e8%93%9d%e7%89%99bluetooth%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/android\/2017\/04\/01\/1453321021.html","title":{"rendered":"Android\u5f00\u53d1\u84dd\u7259Bluetooth\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>1\u3001\u5148\u4e0a\u56fe\uff0c\u518d\u89e3\u91ca\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2017\/04\/device-2017-04-01-111120.png\" alt=\"\" width=\"360\" height=\"640\" class=\"alignnone size-full wp-image-1022\" \/><\/p>\n<p>2\u3001\u57fa\u672c\u4f7f\u7528<br \/>\n2.1\u3001\u68c0\u6d4b\u84dd\u7259\u3001\u6253\u5f00\u84dd\u7259\u53d1\u73b0\u72b6\u6001\u3001\u8f93\u51fa\u5df2\u914d\u5bf9\u8bbe\u5907\u3001\u626b\u63cf\u5468\u8fb9\u84dd\u7259\u8bbe\u5907\uff1b<br \/>\n2.2\u3001\u83b7\u53d6\u5f53\u524d\u673a\u5668\u84dd\u7259\u8bbe\u5907\u4f7f\u7528BluetoothAdapter.getDefaultAdapter()\u65b9\u6cd5\uff1b<br \/>\n2.3\u3001\u4f7f\u7528\u84dd\u7259\u8bbe\u5907\u65f6\u9700\u8981\u5728AndroidManifest.xml\u4e2d\u6388\u6743<\/p>\n<pre line=\"1\" lang=\"java\"><uses-permission android:name=\"android.permission.BLUETOOTH\" \/>\n<uses-permission android:name=\"android.permission.BLUETOOTH_ADMIN\" \/><\/pre>\n<p>\u524d\u8005\u4e3a\u83b7\u53d6\u914d\u5bf9\u8bbe\u5907\u65f6\u4f7f\u7528\uff0c\u540e\u8005\u4e3a\u626b\u63cf\u5468\u8fb9\u84dd\u7259\u8bbe\u5907\u65f6\u4f7f\u7528\uff1b<br \/>\n2.4\u3001\u626b\u63cf\u84dd\u7259\u8bbe\u5907\u4e3a\u5f02\u6b65\u65b9\u6cd5\uff0c\u6bcf\u53d1\u73b0\u4e00\u4e2a\u5468\u8fb9\u8bbe\u5907\u4f1a\u53d1\u9001\u4e00\u6761\u76f8\u5173\u5e7f\u64ad\uff1b<br \/>\n2.5\u3001\u76d1\u542c\u626b\u63cf\u7ed3\u679c\u9700\u8981\u6ce8\u518c\u5e7f\u64adBluetoothDevice.ACTION_FOUND<\/p>\n<p>3\u3001\u76f8\u5173\u4ee3\u7801<!--more--><br \/>\nactivity_main.xml<\/p>\n<pre line=\"1\" lang=\"java\"><?xml version=\"1.0\" encoding=\"utf-8\"?>\n<LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:orientation=\"vertical\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\"com.yusian.bluetooth.MainActivity\">\n\n    <TextView\n        android:id=\"@+id\/tv_title\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Hello World!\"\n        android:padding=\"10dp\"\/>\n\n    <Button\n        android:id=\"@+id\/btn_blue_dev\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u5df2\u914d\u5bf9\u8bbe\u5907\"\/>\n    <Button\n        android:id=\"@+id\/btn_open_discover\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u6253\u5f00\u84dd\u7259\u53ef\u89c1\u6027\"\/>\n    <Button\n        android:id=\"@+id\/btn_search\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"\u641c\u7d22\" \/>\n\n<\/LinearLayout><\/pre>\n<p>MainActivity<\/p>\n<pre line=\"1\" lang=\"java\">package com.yusian.bluetooth;\n\nimport android.bluetooth.BluetoothAdapter;\nimport android.bluetooth.BluetoothDevice;\nimport android.content.BroadcastReceiver;\nimport android.content.Context;\nimport android.content.Intent;\nimport android.content.IntentFilter;\nimport android.support.v7.app.AppCompatActivity;\nimport android.os.Bundle;\nimport android.util.Log;\nimport android.view.View;\nimport android.widget.TextView;\n\nimport java.util.Iterator;\nimport java.util.Set;\n\npublic class MainActivity extends AppCompatActivity implements View.OnClickListener{\n    private final static String TAG = \"SALog\";\n    private TextView textView = null;\n    private BroadcastReceiver receiver = null;\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        textView = (TextView) findViewById(R.id.tv_title);\n        findViewById(R.id.btn_blue_dev).setOnClickListener(this);\n        findViewById(R.id.btn_search).setOnClickListener(this);\n        findViewById(R.id.btn_open_discover).setOnClickListener(this);\n\n        \/\/ \u6ce8\u518c\u8bbe\u5907\u626b\u63cf\u5e7f\u64ad\uff1a\u521b\u5efa\u5efa\u4e00\u4e2aIntentFilter\u548c\u4e00\u4e2aBroadcastReceiver\n        \/\/ BluetoothDevice.ACTION_FOUND\u4e3a\u901a\u77e5\u540d\u79f0\n        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);\n        receiver = new BroadcastReceiver() {\n            @Override\n            public void onReceive(Context context, Intent intent) {\n                String action = intent.getAction();\n                Log.d(TAG, \"onReceive: \"+action);\n                if (action.equals(BluetoothDevice.ACTION_FOUND)){\n                    \/\/ \u83b7\u53d6\u5e7f\u64ad\u4e2d\u5173\u952e\u5185\u5bb9\uff0c\u901a\u8fc7key\u53d6value\n                    \/\/ BluetoothDevice\u7684Key\u4e3aBluetoothDevice.EXTRA_DEVICE\n                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);\n                    \/\/ \u62fc\u63a5\u5b57\u7b26\u4e32\u663e\u793a\u5230\u6587\u672c\u6846\u63a7\u4ef6\u5f53\u4e2d\n                    StringBuilder string = new StringBuilder();\n                    string.append(textView.getText()+\"\\n\"+device.getAddress());\n                    textView.setText(string.toString());\n                }\n            }\n        };\n        registerReceiver(receiver, filter);\n    }\n\n    @Override\n    public void onClick(View v) {\n        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();\n        \/\/ \u5982\u679c\u672c\u5730\u65e0\u84dd\u7259\u8bbe\u5907\u5219\u76f4\u63a5\u8fd4\u56de\n        if (bluetoothAdapter == null){Log.d(TAG, \"onClick: \u65e0\u84dd\u7259\u8bbe\u5907\uff01\"); return;}\n        \/\/ \u5982\u679c\u84dd\u7259\u8bbe\u5907\u672a\u6253\u5f00\uff0c\u5219\u5f39\u51fa\u63d0\u793a\uff01\uff08\u6253\u5f00\u84dd\u7259\u5f00\u5173\uff01\uff09\n        if (bluetoothAdapter.isEnabled() == false) startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));\n        \/\/ \u5404\u4e2a\u6309\u94ae\u4e8b\u4ef6\u5904\u7406\n        switch (v.getId()){\n            \/\/ \u83b7\u53d6\u5f53\u524d\u5df2\u914d\u5bf9\u8bbe\u5907\n            case R.id.btn_blue_dev:{\n                this.getContentedDevice(bluetoothAdapter);\n            }break;\n            \/\/ \u626b\u63cf\u5468\u8fb9\u84dd\u7259\u8bbe\u5907\n            case R.id.btn_search:{\n                bluetoothAdapter.startDiscovery();\n            }break;\n            case R.id.btn_open_discover:{\n                \/\/ \u6253\u5f00\u84dd\u7259\u8bbe\u5907\u53ef\u89c1\u6027\uff08\u6253\u5f00\u84dd\u7259\u8bbe\u5907\u53ef\u89c1\u6027\uff01\uff09\n                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);\n                intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500);\n                startActivity(intent);\n            }break;\n            default:break;\n        }\n    }\n    \/\/ \u83b7\u53d6\u5df2\u914d\u5bf9\u6210\u529f\u7684\u8bbe\u5907\n    public void getContentedDevice(BluetoothAdapter bluetoothAdapter){\n        \/\/ \u4f7f\u7528Iterator\u904d\u5386\u84dd\u7259\u8bbe\u5907\u96c6\u5408\n        Set<BluetoothDevice> deviceSet = bluetoothAdapter.getBondedDevices();\n        Iterator iterator = deviceSet.iterator();\n        StringBuilder string = new StringBuilder();\n        while (iterator.hasNext()){\n            BluetoothDevice bluetoothDevice = (BluetoothDevice) iterator.next();\n            string.append(string + bluetoothDevice.getAddress());\n        }\n        if(string != null) textView.setText(string.toString());\n    }\n\n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        unregisterReceiver(receiver);\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5148\u4e0a\u56fe\uff0c\u518d\u89e3\u91ca\uff01 2\u3001\u57fa\u672c\u4f7f\u7528 2.1\u3001\u68c0\u6d4b\u84dd\u7259\u3001\u6253\u5f00\u84dd\u7259\u53d1\u73b0\u72b6\u6001\u3001\u8f93\u51fa\u5df2\u914d\u5bf9\u8bbe\u5907\u3001\u626b\u63cf\u5468\u8fb9\u84dd\u7259\u8bbe\u5907\uff1b 2.2\u3001\u83b7\u53d6\u5f53\u524d\u673a\u5668\u84dd\u7259\u8bbe\u5907\u4f7f\u7528BluetoothAdapter.getDefaultAdapter()\u65b9\u6cd5\uff1b 2.3\u3001\u4f7f\u7528\u84dd\u7259\u8bbe\u5907\u65f6\u9700\u8981\u5728AndroidManifest.xml\u4e2d\u6388\u6743 \u524d\u8005\u4e3a\u83b7\u53d6\u914d\u5bf9\u8bbe\u5907\u65f6\u4f7f\u7528\uff0c\u540e\u8005\u4e3a\u626b\u63cf\u5468\u8fb9\u84dd\u7259\u8bbe\u5907\u65f6\u4f7f\u7528\uff1b 2.4\u3001\u626b\u63cf\u84dd\u7259\u8bbe\u5907\u4e3a\u5f02\u6b65\u65b9\u6cd5\uff0c\u6bcf\u53d1\u73b0\u4e00\u4e2a\u5468\u8fb9\u8bbe\u5907\u4f1a\u53d1\u9001\u4e00\u6761\u76f8\u5173\u5e7f\u64ad\uff1b 2.5\u3001\u76d1\u542c\u626b\u63cf\u7ed3\u679c\u9700\u8981\u6ce8\u518c\u5e7f\u64adBluetoothDevice.ACTION_FOUND 3\u3001\u76f8\u5173\u4ee3\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33],"class_list":["post-1021","post","type-post","status-publish","format-standard","hentry","category-android","tag-33"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1021"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1021\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}