久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2627|回復: 0
打印 上一主題 下一主題
收起左側

藍牙控制小車 手機端JAVA程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:113175 發表于 2016-4-9 21:23 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
(二)手機端JAVA程序:
1.布局文件
<RelativeLayout 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:background="@drawable/background1">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="28dp"
        android:text="安卓手機藍牙控制智能小車"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="18dp"
        android:text="重慶郵電大學自動化學院"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp"
        android:text="作者:張露"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignLeft="@+id/textView3"
        android:layout_marginBottom="19dp"
        android:background="@drawable/backgroundzuo"
        android:text="左轉" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/backgroundhou"
        android:text="后退" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignLeft="@+id/button2"
        android:background="@drawable/backgroundqian"
        android:text="前進" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/button1"
        android:background="@drawable/backgroundyou"
        android:text="右轉" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button3"
        android:layout_alignBottom="@+id/button3"
        android:layout_centerHorizontal="true"
        android:text="停止" />

</RelativeLayout>


4.Activity文件
public class Bluetooth_lightActivity extends Activity implements OnClickListener{
        
        String information;
        public byte[] message = new byte[1];
        private Vibrator vibrator;

        private BluetoothAdapter btadapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice btDevice;
        BluetoothSocket bluetoothSocket = null;
        //BluetoothReceiver mReceiver;
        OutputStream tmpOut = null;
        private static final UUID MY_UUID_SECURE = UUID
                        .fromString("00001101-0000-1000-8000-00805F9B34FB");
        private String blueAddress="00:12:10:31:01:70";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_light);
                if (btadapter != null) {
                        if (!btadapter.isEnabled()) {
                                // 通過這個方法來請求打開我們的藍牙設備
                                Intent intent = new Intent(
                                                BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                startActivity(intent);
                        }
                } else {
                        System.out.println("本地設備驅動異常!");
                }
                Button qianjin = (Button) findViewById(R.id.button1);
                Button houtui = (Button) findViewById(R.id.button2);
                Button zuozhuan = (Button) findViewById(R.id.button3);
                Button youzhuan = (Button) findViewById(R.id.button4);
                Button tingzhi = (Button) findViewById(R.id.button5);
               
                qianjin.setOnClickListener((OnClickListener) this);
                houtui.setOnClickListener((OnClickListener) this);
                zuozhuan.setOnClickListener((OnClickListener) this);
                youzhuan.setOnClickListener((OnClickListener) this);
                tingzhi.setOnClickListener((OnClickListener) this);
               
    }

        public void bluesend(byte[] msg) {
               
                try {
                        tmpOut = bluetoothSocket.getOutputStream();
                        Log.d("send", Arrays.toString(msg));
                        tmpOut.write(msg);

                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public void vibrator()
        {
                vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(10);
        }
        
        /**
         * 按鈕點擊事件.
         * @param v
         */
        public void onClick(View v) {
                switch (v.getId()) {
                //轉寫按鈕
                case R.id.button1:
                        message[0] = (byte) 0x41;                        
                        vibrator();
                        Toast.makeText(this, "前進", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                //設置按鈕
                case R.id.button2:
                        message[0] = (byte) 0x44;
                        vibrator();
                        Toast.makeText(this, "后退", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                case R.id.button3:
                        message[0] = (byte) 0x43;
                        vibrator();
                        Toast.makeText(this, "左轉", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                case R.id.button4:
                        message[0] = (byte) 0x42;
                        vibrator();
                        Toast.makeText(this, "右轉", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                case R.id.button5:
                        message[0] = (byte) 0x61;
                        vibrator();                        
                        Toast.makeText(this, "停止", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                        
                default:
                        break;
                }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_bluetooth_light, menu);
        return true;
    }
        @Override
        protected void onDestroy() {
                // TODO Auto-generated method stub
                super.onDestroy();
                try {
                        bluetoothSocket.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                //unregisterReceiver(mReceiver);
        }

        @Override
        protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
//                Set<BluetoothDevice> devices = btadapter.getBondedDevices();
                btDevice = btadapter.getRemoteDevice(blueAddress);
                try {
                        bluetoothSocket = btDevice
                                        .createRfcommSocketToServiceRecord(MY_UUID_SECURE);
                        Log.d("ChuyingjihuaActivity", "開始連接...");
                        bluetoothSocket.connect();
                        Log.d("ChuyingjihuaActivity", "完成連接");

                } catch (IOException e) {
                        // TODO Auto-generated catch block

                        e.printStackTrace();
                }
        }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日韩视频在线免费观看 | 日本一卡精品视频免费 | 亚洲日本视频 | 这里有精品 | 中文字幕日韩av | 在线午夜 | 超碰av免费 | 亚洲精品9999 | 国产精品一区一区三区 | 欧美精品网站 | 久久久久久久久久久久久91 | 韩国久久 | 天天射视频 | 精品无码久久久久久国产 | 欧美一级久久 | 欧美色图综合网 | 国产色网| 91精品国产91久久综合桃花 | 亚洲精品在线视频 | 中文字幕精品一区久久久久 | 欧美 日韩 视频 | 国产精品99久久久久久www | a免费视频| 久久久久国产精品午夜一区 | 男女激情网站免费 | 成人午夜视频在线观看 | 日本福利片 | 无码日韩精品一区二区免费 | 久久精品国产亚洲一区二区三区 | 欧美日韩综合一区 | 天天综合天天 | 中文字幕在线剧情 | 365夜爽爽欧美性午夜免费视频 | 亚洲xx在线| 国产成人网 | 天天插天天操 | 久久精品一区二区 | 久久久不卡网国产精品一区 | 国产精品成人品 | 亚洲狠狠丁香婷婷综合久久久 | 99精品欧美一区二区三区 |