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

標題: 藍牙控制小車 手機端JAVA程序 [打印本頁]

作者: 就是這么帥    時間: 2016-4-9 21:23
標題: 藍牙控制小車 手機端JAVA程序
(二)手機端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();
                }
        }
}







歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 日韩av一区二区在线观看 | 亚洲国产免费 | 黄色三级视频在线观看 | 亚洲国产黄色 | 欧美激情xxx | 国产成人精品免费视频 | 欧美日韩在线不卡 | 婷婷在线视频 | 国产成人av在线 | 91免费观看视频 | 欧洲精品一区二区三区 | 黄色一级大片在线免费看国产一 | 福利av在线| a级片在线观看 | 国产第三页 | 久久精品在线观看 | 免费在线 | 欧美日韩在线免费观看 | 成人午夜网站 | 欧美三级韩国三级日本三斤在线观看 | 国产成人精品毛片 | 国产免费一区二区三区 | 国产精品成人在线 | 成年人视频在线播放 | 亚洲国产精品久久久 | 中文字幕一区二区三区在线观看 | 日本一级大毛片a一 | 国产伦精品一区二区三区免.费 | 五月综合色 | 欧美日韩成人一区二区三区 | 欧美综合色 | av观看免费 | 天堂8中文 | 色哟哟一区二区三区 | 亚洲黄色在线视频 | 欧美日韩一区二区三区视频 | 欧美精品一二三区 | 免费毛片观看 | 成人午夜在线 | 99九九久久 | 欧美黄网站 |