找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2668|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

藍(lán)牙控制小車 手機(jī)端JAVA程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:113175 發(fā)表于 2016-4-9 21:23 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
(二)手機(jī)端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="安卓手機(jī)藍(lán)牙控制智能小車"
        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="重慶郵電大學(xué)自動(dòng)化學(xué)院"
        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="左轉(zhuǎn)" />

    <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="前進(jìn)" />

    <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="右轉(zhuǎn)" />

    <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()) {
                                // 通過(guò)這個(gè)方法來(lái)請(qǐng)求打開(kāi)我們的藍(lán)牙設(shè)備
                                Intent intent = new Intent(
                                                BluetoothAdapter.ACTION_REQUEST_ENABLE);
                                startActivity(intent);
                        }
                } else {
                        System.out.println("本地設(shè)備驅(qū)動(dòng)異常!");
                }
                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);
        }
        
        /**
         * 按鈕點(diǎn)擊事件.
         * @param v
         */
        public void onClick(View v) {
                switch (v.getId()) {
                //轉(zhuǎn)寫按鈕
                case R.id.button1:
                        message[0] = (byte) 0x41;                        
                        vibrator();
                        Toast.makeText(this, "前進(jìn)", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                //設(shè)置按鈕
                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, "左轉(zhuǎn)", Toast.LENGTH_LONG).show();
                        bluesend(message);
                        break;
                case R.id.button4:
                        message[0] = (byte) 0x42;
                        vibrator();
                        Toast.makeText(this, "右轉(zhuǎn)", 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", "開(kāi)始連接...");
                        bluetoothSocket.connect();
                        Log.d("ChuyingjihuaActivity", "完成連接");

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

                        e.printStackTrace();
                }
        }
}


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

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表