{"id":53657,"date":"2026-07-14T11:05:04","date_gmt":"2026-07-14T03:05:04","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=53657"},"modified":"2026-07-14T11:05:04","modified_gmt":"2026-07-14T03:05:04","slug":"%e5%88%b6%e4%bd%9c%e5%ae%89%e5%8d%93%e8%93%9d%e7%89%99app%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2026\/53657\/","title":{"rendered":"\u5236\u4f5c\u5b89\u5353\u84dd\u7259app\u5982\u4f55\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>\u5236\u4f5c\u5b89\u5353\u84dd\u7259App\u7684\u57fa\u672c\u539f\u7406\u5c31\u662f\u4f7f\u7528Android SDK\u4e2d\u63d0\u4f9b\u7684Bluetooth API\uff0c\u901a\u8fc7\u84dd\u7259\u6a21\u5757\u5728\u8bbe\u5907\u4e4b\u95f4\u5efa\u7acb\u65e0\u7ebf\u901a\u4fe1\uff0c\u5e76\u5b9e\u73b0\u6570\u636e\u4f20\u8f93\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Android Studio\u5f00\u53d1\u4e00\u4e2a\u4e0e\u5916\u56f4\u84dd\u7259\u8bbe\u5907\u8fdb\u884c\u8fde\u63a5\u5e76\u53d1\u9001\u6570\u636e\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>1. \u521b\u5efa\u65b0\u9879\u76ee<\/p>\n<p>\u5728Android Studio\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u9879\u76ee\u3002\u5728\u9879\u76ee\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6253\u5f00build.gradle\u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4f9d\u8d56\u9879\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>dependencies {<\/p>\n<p>    implementation &#8216;com.android.support:appcompat-v7:28.0.0&#8217;<\/p>\n<p>    implementation &#8216;com.android.support.constraint:constraint-layout:1.1.3&#8217;<\/p>\n<p>    implementation &#8216;com.android.support:design:28.0.0&#8217;<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>2. \u5e03\u5c40\u8bbe\u8ba1<\/p>\n<p>\u5728activity_main.xml\u4e2d\u6dfb\u52a0\u4e24\u4e2a\u6309\u94ae\uff0c\u4e00\u4e2a\u7528\u4e8e\u641c\u7d22\u53ef\u7528\u8bbe\u5907\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u8fde\u63a5\u9009\u5b9a\u7684\u8bbe\u5907\u3002<\/p>\n<p>&#8220;`<\/p>\n<\/p>\n<p>    android:id=&#8221;@+id\/button_search&#8221;<\/p>\n<p>    android:layout_width=&#8221;wrap_content&#8221;<\/p>\n<p>    android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>    android:text=&#8221;\u641c\u7d22&#8221;<\/p>\n<p>    app:layout_constraintBottom_toBottomOf=&#8221;parent&#8221;<\/p>\n<p>   <a href=\"https:\/\/www.anzhuoe.com\/\">\u5b89\u5353app<\/a> app:layout_constraintHorizontal_bias=&#8221;0.5&#8243;<\/p>\n<p>    app:layout_constraintLeft_toLeftOf=&#8221;parent&#8221;<\/p>\n<p>    app:layout_constraintRight_toLeftOf=&#8221;@+id\/button_connect&#8221;<\/p>\n<p>    app:layout_constraintTop_toTopOf=&#8221;parent&#8221;<\/p>\n<p>    app:layout_constraintVertical_bias=&#8221;0.5&#8243; \/&gt;<\/p>\n<\/p>\n<p>    android:id=&#8221;@+id\/button_connect&#8221;<\/p>\n<p>    android:layout_width=&#8221;wrap_content&#8221;<\/p>\n<p>    android:layout_height=&#8221;wrap_content&#8221;<\/p>\n<p>    android:text=&#8221;\u8fde\u63a5&#8221;<\/p>\n<p>    app:layout_constraintBottom_toBottomOf=&#8221;parent&#8221;<\/p>\n<p>    app:layout_constraintHorizontal_bias=&#8221;0.5&#8243;<\/p>\n<p>    app:layout_constraintRight_toRightOf=&#8221;parent&#8221;<\/p>\n<p>    app:layout_constraintTop_toTopOf=&#8221;parent&#8221;<\/p>\n<p>    app:layout_constraintVertical_bias=&#8221;0.5&#8243; \/&gt;<\/p>\n<p>&#8220;`<\/p>\n<p>3. \u5b9a\u4e49Java\u7c7b<\/p>\n<p>\u5728MainActivity.java\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e24\u4e2a\u6309\u94ae\u7684\u76d1\u542c\u5668\uff0c\u5e76\u4f7f\u7528BluetoothAdapter\u7c7b\u6765\u641c\u7d22\u548c\u8fde\u63a5\u8bbe\u5907\u3002\u6b64\u5916\uff0c\u4e5f\u9700\u8981\u4f7f\u7528BluetoothSocket\u7c7b\u4e0e\u5916\u56f4\u8bbe\u5907\u8fdb\u884c\u65e0\u7ebf\u901a\u4fe1\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>public class MainActivity extends AppCompatAc<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.myapp.ltd\/ag\/8\/sucai\/1.jpg\" \/><\/figure>\n<\/p>\n<p>tivity {<\/p>\n<\/p>\n<p>    private final static int REQUEST_ENABLE_BT = 1;<\/p>\n<p>    private BluetoothAdapter bluetoothAdapter;<\/p>\n<p>    private Set pairedDevices;<\/p>\n<p><p>    private ArrayAdapter devicesArrayAdapter<a href=\"https:\/\/www.anzhuoe.com\/\">\u5b89\u5353APP\u5f00\u53d1<\/a>;<\/p>\n<p><p>    private ListView listView;<\/p>\n<p>    private BluetoothDevice selectedDevice;<\/p>\n<p>    private BluetoothSocket mmSocket;<\/p>\n<p>    private OutputStream mmOutputStream;<\/p>\n<\/p>\n<p>    @Override<\/p>\n<p>    protected void onCreate(Bundle savedInstanceState) {<\/p>\n<p>        super.onCreate(savedInstanceState);<\/p>\n<p>        setContentView(R.layout.activity_main);<\/p>\n<\/p>\n<p>        listView = findViewById(R.id.listView);<\/p>\n<\/p>\n<p>        checkBluetoothState();<\/p>\n<\/p>\n<p>        \/\/ \u8bbe\u7f6e\u641c\u7d22\u6309\u94ae\u76d1\u542c\u5668<\/p>\n<p>        Button searchButton = (Button) findViewById(R.id.button_search);<\/p>\n<p>        searchButton.setOnClickListener(new View.OnClickListener() {<\/p>\n<p>            @Override<\/p>\n<p>            public void onClick(View view) {<\/p>\n<p>                discoverDevices();<\/p>\n<p>            }<\/p>\n<p>        });<\/p>\n<\/p>\n<p>        \/\/ \u8bbe\u7f6e\u8fde\u63a5\u6309\u94ae\u76d1\u542c\u5668<\/p>\n<p>        Button connectButton = (Button) findViewById(R.id.button_connect);<\/p>\n<p>        connectButton.setOnClickListener(new View.OnClickListener() {<\/p>\n<p>            @Override<\/p>\n<p>            public void onClick(View view) {<\/p>\n<p>                connectToDevice(selectedDevice);<\/p>\n<p>            }<\/p>\n<p>        });<\/p>\n<p>    }<\/p>\n<\/p>\n<p>    private void checkBluetoothState() {<\/p>\n<p>        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();<\/p>\n<\/p>\n<p>        if (bluetoothAdapter == null) {<\/p>\n<p>            Toast.makeText(this, &#8220;\u84dd\u7259\u4e0d\u53ef\u7528&#8221;, Toast.LENGTH_SHORT).show();<\/p>\n<p>            finish();<\/p>\n<p>        } else {<\/p>\n<p>            if (!bluetoothAdapter.isEnabled()) {<\/p>\n<p>                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);<\/p>\n<p>                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<\/p>\n<p>    private void discoverDevices() {<\/p>\n<p>        if (bluetoothAdapter.isDiscovering()) {<\/p>\n<p>            bluetoothAdapter.cancelDiscovery();<\/p>\n<p>        }<\/p>\n<\/p>\n<p>        bluetoothAdapter.startDiscovery();<\/p>\n<\/p>\n<p>        BroadcastReceiver discoveryResult = new BroadcastReceiver() {<\/p>\n<p>            @Override<\/p>\n<p>            public void onReceive(Context context, Intent intent) {<\/p>\n<p>                String action = intent.getAction();<\/p>\n<p>                if (BluetoothDevice.ACTION_FOUND.equals(action)) {<\/p>\n<p>                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);<\/p>\n<p>                    devicesArrayAdapter.add(device.getName() + &#8220;\\n&#8221; + device.getAddress());<\/p>\n<p>                }<\/p>\n<p>            }<\/p>\n<p>        };<\/p>\n<\/p>\n<p>        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);<\/p>\n<p>        registerReceiver(discoveryResult, filter);<\/p>\n<p>        devicesArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);<\/p>\n<p><p>        listView.setAdapter(devicesArrayAdapter);<\/p>\n<p>    }<\/p>\n<\/p>\n<p>    private void connectToDevice(BluetoothDevice device) {<\/p>\n<p>        UUID uuid = UUID.fromString(&#8220;00001101-0000-1000-8000-00805f9b34fb&#8221;);<\/p>\n<\/p>\n<p>        try {<\/p>\n<p>            mmSocket = device.createRfcommSocketToServiceRecord(uuid);<\/p>\n<p>            mmSocket.connect();<\/p>\n<p>            mmOutputStream = mmSocket.getOutputStream();<\/p>\n<p>        } catch (IOException e) {<\/p>\n<p>            e.printStackTrace();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>4. \u5728Manifest\u6587\u4ef6\u4e2d\u6dfb\u52a0\u84dd\u7259\u6743\u9650<\/p>\n<p>&#8220;`<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>5. \u8fd0\u884c\u548c\u6d4b\u8bd5<\/p>\n<p>\u5728\u8fd0\u884c\u548c\u6d4b\u8bd5\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u4f60\u7684Android\u8bbe\u5907\u5df2\u542f\u7528\u84dd\u7259\uff0c\u4ee5\u53ca\u9644\u8fd1\u6709\u5176\u4ed6\u84dd\u7259\u8bbe\u5907\u53ef\u4f9b\u8fde\u63a5\u3002<\/p>\n<p>\u7ed3\u8bba\uff1a\u901a\u8fc7\u8fd9\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5230\u5236\u4f5c\u5b89\u5353\u84dd\u7259App\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Android SDK\u4e2d\u63d0\u4f9b\u7684\u5404\u79cd\u7c7b\u548cAPI\uff0c\u81ea\u5df1\u8bbe\u8ba1\u51fa\u66f4\u52a0\u5f3a\u5927\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5236\u4f5c\u5b89\u5353\u84dd\u7259App\u7684\u57fa\u672c\u539f\u7406\u5c31\u662f\u4f7f\u7528Android SDK\u4e2d\u63d0\u4f9b\u7684Bluetooth API\uff0c\u901a\u8fc7\u84dd\u7259\u6a21\u5757\u5728\u8bbe\u5907\u4e4b\u95f4\u5efa\u7acb\u65e0\u7ebf\u901a\u4fe1\uff0c\u5e76\u5b9e\u73b0\u6570\u636e\u4f20\u8f93\u7684\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Androi<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[40760,40761,40735,3249,331],"class_list":{"0":"post-53657","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-app","9":"tag-40735","10":"tag-3249","11":"tag-331"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/53657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=53657"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/53657\/revisions"}],"predecessor-version":[{"id":53677,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/53657\/revisions\/53677"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=53657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=53657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=53657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}